Skip to content

Commit

Permalink
[FEAT] #11 return access/refresh token
Browse files Browse the repository at this point in the history
  • Loading branch information
nykoh2001 committed May 31, 2024
1 parent 5eaf75e commit 5315238
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
public class JwtTokenProvider {
private static final String USER_ID = "userId";

private static final Long ACCESS_TOKEN_EXPIRATION_TIME = 24 * 60 * 60 * 1000L * 14;
private static final Long ACCESS_TOKEN_EXPIRATION_TIME = 24 * 60 * 60 * 1000L * 7;
private static final Long REFRESH_TOKEN_EXPIRATION_TIME = 24 * 60 * 60 * 1000L * 14;

@Value("${jwt.secret}")
private String JWT_SECRET;
Expand All @@ -32,7 +33,11 @@ public class JwtTokenProvider {
* - authorities: null
* */
public String issueAccessToken(final Authentication authentication) {
return generateToken(authentication, ACCESS_TOKEN_EXPIRATION_TIME);
return generateAccessToken(authentication, ACCESS_TOKEN_EXPIRATION_TIME);
}

public String issueRefreshToken() {
return generateRefreshToken(REFRESH_TOKEN_EXPIRATION_TIME);
}

/* 토큰 생성 로직
Expand All @@ -45,7 +50,7 @@ public String issueAccessToken(final Authentication authentication) {
- signWith: 서명 설정 및 암호화
- compact: 토큰 생성
*/
public String generateToken(Authentication authentication, Long tokenExpirationTime) {
public String generateAccessToken(Authentication authentication, Long tokenExpirationTime) {
final Date now = new Date();
final Claims claims = Jwts.claims()
.setIssuedAt(now)
Expand All @@ -60,6 +65,18 @@ public String generateToken(Authentication authentication, Long tokenExpirationT
.compact();
}

public String generateRefreshToken(Long tokenExpirationTime) {
final Date now = new Date();
final Claims claims = Jwts.claims()
.setIssuedAt(now)
.setExpiration(new Date(now.getTime() + tokenExpirationTime)); // 만료 시간

return Jwts.builder()
.setClaims(claims)
.signWith(getSigningKey())
.compact();
}

/* 서명 생성
* Base64 인코딩된 JWT_SECRET을 SecretKey로 변환
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public UserJoinResponse createMember(
String accessToken = jwtTokenProvider.issueAccessToken(
UserAuthentication.createUserAuthentication(memberId)
);
return UserJoinResponse.of(accessToken, memberId.toString());
String refreshToken = jwtTokenProvider.issueRefreshToken();

return UserJoinResponse.of(accessToken, refreshToken, memberId.toString());
}

/* private -> protected로 다른 서비스 레이어에서 호출할 수 있도록 수정 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

public record UserJoinResponse(
String accessToken,
String refreshToken,
String userId
) {

public static UserJoinResponse of(
String accessToken,
String refreshToken,
String userId
) {
return new UserJoinResponse(accessToken, userId);
return new UserJoinResponse(accessToken, refreshToken, userId);
}
}

0 comments on commit 5315238

Please sign in to comment.