Skip to content

Commit

Permalink
Merge pull request #111 from UnivApp/feature/jwt-refactor2
Browse files Browse the repository at this point in the history
refactor: access token, refresh token 유효기간 리팩토링
  • Loading branch information
nyeroni authored Oct 7, 2024
2 parents 8369e51 + 066a4fc commit e90726b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/main/java/yerong/wedle/oauth/dto/TokenResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
public class TokenResponse {

private String accessToken;
private LocalDateTime accessTokenExpiresIn;
private long accessTokenExpiresIn;

private String refreshToken;
private LocalDateTime refreshTokenExpiresIn;
private long refreshTokenExpiresIn;

}
15 changes: 9 additions & 6 deletions src/main/java/yerong/wedle/oauth/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,30 @@ public JwtProvider(@Value("${jwt.secret_key}") String secretKey){
public TokenResponse generateTokenDto(String socialId){
long now = System.currentTimeMillis();

LocalDateTime accessTokenExpiresIn = LocalDateTime.ofInstant(Instant.ofEpochMilli(now + accessTokenExpireTime), ZoneId.systemDefault());
LocalDateTime refreshTokenExpiresIn = LocalDateTime.ofInstant(Instant.ofEpochMilli(now + refreshTokenExpireTime), ZoneId.systemDefault());
Date accessTokenExpiration = new Date(now + accessTokenExpireTime);
Date refreshTokenExpiration = new Date(now + refreshTokenExpireTime);

String accessToken = Jwts.builder()
.setSubject(socialId)
.claim(AUTHORITIES_KEY, Role.USER.getKey())
.setExpiration(Date.from(accessTokenExpiresIn.atZone(ZoneId.systemDefault()).toInstant())) // Date로 변환
.setExpiration(accessTokenExpiration)
.signWith(key, SignatureAlgorithm.HS256)
.compact();

log.info("Access Token Expiration Time: " + accessTokenExpiration);
log.info("Refresh Token Expiration Time: " + refreshTokenExpiration);

// Refresh Token 생성
String refreshToken = Jwts.builder()
.setExpiration(Date.from(refreshTokenExpiresIn.atZone(ZoneId.systemDefault()).toInstant())) // Date로 변환
.setExpiration(refreshTokenExpiration) // Date로 변환
.signWith(key, SignatureAlgorithm.HS256)
.compact();

return TokenResponse.builder()
.accessToken(accessToken)
.accessTokenExpiresIn(accessTokenExpiresIn)
.accessTokenExpiresIn(accessTokenExpireTime)
.refreshToken(refreshToken)
.refreshTokenExpiresIn(refreshTokenExpiresIn)
.refreshTokenExpiresIn(refreshTokenExpireTime)
.build();
}

Expand Down

0 comments on commit e90726b

Please sign in to comment.