jwt ν ν° μ μΆκ°νλ λμ€
μ΄λ―Έ μ¬μ©μ€μΈ μλΉμ€κ° μ¬μ©νκ³ μλκ±Έ μΆμννλ μμ μ€μ μ λ€λ¦μ μ¬μ©ν νκΈ°μ΄λ€.
μλΉμ€ λ§λ€ λ€λ₯Έ ν ν°μ μ§ μκ°, νμ΄λ‘λλ₯Ό κ°κ³ μλλ°
μ€λ²λΌμ΄λ©μ μ¬μ©νμλλ° μ½λ리뷰 λ κ°μ κΈ°λ₯μ νλ©΄μ μ½κ°μ νλμΆκ°λ§ λλκ²μ΄λΌμ μ λ€λ¦μ μ¬μ©ν΄λ³΄λκ² μ΄λ»λλ νΌλλ°±μ λ°μλ€. λ¬Όλ‘ μ²μ μ¨λ³΄λκ±°λΌμ μκ°μ΄ μ€λ κ±Έλ Έλ€. μ¬λ―Έμμκ³ μ μΈκ³μλ€ (μ λλ‘ μ΄κ²λ μλμ§λ§)
μλμ κ°μ΄ μ λ€λ¦ ν΄λμ€λ₯Ό μμ±νκ³ issue (ν ν°λ°ν) μ μ€ννμ λ μ λ€λ¦ νμ μΌλ‘ μμ± νμλ€
@Data
@AllArgsConstructor
public class TokenPayload<T> {
private T tokenPayload;
public T getTokenPayload() {
return tokenPayload;
}
}
@Transactional
@Override
public JsonWebTokenDto issue(IssueReqeust request) {
User user = findByIdAndPwd(request.getId(),request.getPwd());
TokenPayloadDto payload = getTokenPayloadDto(user);
return jwtTokenService.generateTokens(new TokenPayload<>(payload));
}
μ λ¬λ°μ payload λ₯Ό claim μ μ μ₯νλ €λ©΄ payload.getTokenPayload() λ‘ κ°κ³ μ€λ©΄ λλ€.
κ·Έλ¬λ©΄ Token μ μμ±νλ©΄μ μλμ κ°μ΄ μ΄μκ² λμ¨λ€.
public <T> GeneratedTokenDto createAccessToken(@NotNull TokenPayload<T> payload) {
// ...
JwtBuilder jwtBuilder = Jwts.builder()
.setSubject(subject)
.claim(Constants.JWT_PRIVATE_CLAIMS_MEMBER_KEY, payload.getTokenPayload())
.setIssuedAt(issuedAt)
.setExpiration(expiration).setHeaderParam(Header.TYPE, Header.JWT_TYPE)
.signWith(SignatureAlgorithm.HS256, accessSecretKeyByte);
// ...
return GeneratedTokenDto.builder().tokenType(tokenType).subject(subject)
.issueAt(issuedAt).expiration(expiration).token(jwtBuilder.compact()).build();
}
{
"sub": "1",
"companyMember": {
"id": 1,
"branchId": 123,
"serviceId": 4567
},
"iat": 1678784812,
"exp": 1678785412,
"role": [
"ADMIN"
]
}
'π WEB > JAVA' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
LocalDate isEquals(), equals() (0) | 2023.12.14 |
---|---|
λ©λͺ¨λ¦¬ μ μ½ μ΅κ΄ λ€μ΄κΈ° (Java Programming) (0) | 2023.04.23 |
νλ©΄μ μνλ μλ¬λ©μμ§ λ³΄λ΄κΈ° (1) | 2023.03.09 |
λλ§ μ΄λ €μ΄ μμΈμ²λ¦¬ (0) | 2022.05.19 |
Enum Type μμ Function Interface νμ© (0) | 2022.04.01 |