From 580528001ed9d3064d6ae8f59de9a908ae0ca6f3 Mon Sep 17 00:00:00 2001 From: Anna-Jin Date: Mon, 18 Jul 2022 21:58:03 +0900 Subject: [PATCH] =?UTF-8?q?#17=20[Update]=20=ED=86=A0=ED=81=B0=20=EC=9E=AC?= =?UTF-8?q?=EB=B0=9C=EA=B8=89=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 토큰 만료 시간이 5분 남았을 때부터 재발급 가능하게 변경. 프론트에서 setTimeout 설정을 통해 일정 시간마다 토큰을 재발급받도록 변경함. --- .../login/jwt/service/TokenService.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/login/jwt/service/TokenService.java b/src/main/java/com/mpnp/baechelin/login/jwt/service/TokenService.java index 6590a89..f947391 100644 --- a/src/main/java/com/mpnp/baechelin/login/jwt/service/TokenService.java +++ b/src/main/java/com/mpnp/baechelin/login/jwt/service/TokenService.java @@ -30,18 +30,25 @@ public class TokenService { private final AuthTokenProvider tokenProvider; private final UserRefreshTokenRepository userRefreshTokenRepository; private final static long THREE_DAYS_MSEC = 259200000; + private final static long FIVE_MINIUTE_MSEC = 300000; private final static String REFRESH_TOKEN = "refresh_token"; - public AuthResponse refreshToken(HttpServletRequest request, HttpServletResponse response) { + + public AuthResponse refreshToken(HttpServletRequest request, HttpServletResponse response) { String accessToken = HeaderUtil.getAccessToken(request); AuthToken authToken = tokenProvider.convertAuthToken(accessToken); Claims claims = authToken.getExpiredTokenClaims(); + // 토큰 유효시간 계산을 위한 현재시간 가져오기 + Date now = new Date(); + // access token 유효시간 + long AccessValidTime = claims.getExpiration().getTime() - now.getTime(); + // 유효한 access token 인지, 만료된 token 인지 확인 if (authToken.getExpiredTokenClaims() == null) { return AuthResponse.invalidAccessToken(); } else { - if (claims == null) { + if (AccessValidTime >= FIVE_MINIUTE_MSEC) { return AuthResponse.notExpiredTokenYet(); } } @@ -49,7 +56,7 @@ public AuthResponse refreshToken(HttpServletRequest request, HttpServletResponse String userId = claims.getSubject(); RoleType roleType = RoleType.of(claims.get("role", String.class)); - // refresh token + // refresh token을 cookie에서 가져온다. String refreshToken = CookieUtil.getCookie(request, REFRESH_TOKEN) .map(Cookie::getValue) .orElse((null)); @@ -59,26 +66,28 @@ public AuthResponse refreshToken(HttpServletRequest request, HttpServletResponse return AuthResponse.invalidRefreshToken(); } - // userId refresh token 으로 DB 확인 + // userId와 refresh token 으로 DB 확인 UserRefreshToken userRefreshToken = userRefreshTokenRepository.findBySocialIdAndRefreshToken(userId, refreshToken); if (userRefreshToken == null) { return AuthResponse.invalidRefreshToken(); } - Date now = new Date(); + // Access token 재발급 AuthToken newAccessToken = tokenProvider.createAuthToken( userId, roleType.getCode(), new Date(now.getTime() + appProperties.getAuth().getTokenExpiry()) ); - long validTime = authRefreshToken.getTokenClaims().getExpiration().getTime() - now.getTime(); + // TODO 예외 처리 하기 + long RefreshValidTime = authRefreshToken.getTokenClaims().getExpiration().getTime() - now.getTime(); // refresh 토큰 기간이 3일 이하로 남은 경우, refresh 토큰 갱신 - if (validTime <= THREE_DAYS_MSEC) { - // refresh 토큰 설정 + if (RefreshValidTime <= THREE_DAYS_MSEC) { + // refresh 토큰 유효기간 가져오기 long refreshTokenExpiry = appProperties.getAuth().getRefreshTokenExpiry(); + // refresh 토큰 생성 authRefreshToken = tokenProvider.createAuthToken( appProperties.getAuth().getTokenSecret(), new Date(now.getTime() + refreshTokenExpiry)