From 236cb26f16a661f823b6b63e01acd6aa8bbba037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=8E=E1=85=A1=E1=86=AB=E1=84=8C?= =?UTF-8?q?=E1=85=B5=E1=86=AB?= Date: Fri, 30 Dec 2022 16:34:15 +0900 Subject: [PATCH 1/3] =?UTF-8?q?:recycle:=20::=20[credentials]=20refreshTok?= =?UTF-8?q?en=20=EB=A7=8C=EB=A3=8C=20403=20=EC=9C=BC=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/RefreshTokenExpiredException.java | 13 +++++++++++++ .../credential/service/CredentialService.java | 6 ++++-- .../global/error/exception/ErrorCode.java | 2 ++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/depromeet/knockknockbackend/domain/credential/exception/RefreshTokenExpiredException.java diff --git a/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/exception/RefreshTokenExpiredException.java b/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/exception/RefreshTokenExpiredException.java new file mode 100644 index 00000000..0fc04f80 --- /dev/null +++ b/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/exception/RefreshTokenExpiredException.java @@ -0,0 +1,13 @@ +package io.github.depromeet.knockknockbackend.domain.credential.exception; + + +import io.github.depromeet.knockknockbackend.global.error.exception.ErrorCode; +import io.github.depromeet.knockknockbackend.global.error.exception.KnockException; + +public class RefreshTokenExpiredException extends KnockException { + public static final KnockException EXCEPTION = new RefreshTokenExpiredException(); + + private RefreshTokenExpiredException() { + super(ErrorCode.REGISTER_EXPIRED_TOKEN); + } +} diff --git a/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java b/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java index 48696aea..d558fe91 100644 --- a/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java +++ b/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java @@ -5,6 +5,7 @@ import io.github.depromeet.knockknockbackend.domain.credential.domain.repository.RefreshTokenRedisEntityRepository; import io.github.depromeet.knockknockbackend.domain.credential.exception.AlreadySignUpUserException; import io.github.depromeet.knockknockbackend.domain.credential.exception.ForbiddenUserException; +import io.github.depromeet.knockknockbackend.domain.credential.exception.RefreshTokenExpiredException; import io.github.depromeet.knockknockbackend.domain.credential.presentation.dto.request.RegisterRequest; import io.github.depromeet.knockknockbackend.domain.credential.presentation.dto.response.AfterOauthResponse; import io.github.depromeet.knockknockbackend.domain.credential.presentation.dto.response.AuthTokensResponse; @@ -101,13 +102,14 @@ private String generateRefreshToken(Long userId) { // 토큰 리프레쉬 하기 public AuthTokensResponse tokenRefresh(String requestRefreshToken) { - Long userId = jwtTokenProvider.parseRefreshToken(requestRefreshToken); Optional entityOptional = refreshTokenRedisEntityRepository.findByRefreshToken(requestRefreshToken); RefreshTokenRedisEntity refreshTokenRedisEntity = - entityOptional.orElseThrow(() -> InvalidTokenException.EXCEPTION); + entityOptional.orElseThrow(() -> RefreshTokenExpiredException.EXCEPTION); + + Long userId = jwtTokenProvider.parseRefreshToken(requestRefreshToken); if (!userId.toString().equals(refreshTokenRedisEntity.getId())) { throw InvalidTokenException.EXCEPTION; diff --git a/src/main/java/io/github/depromeet/knockknockbackend/global/error/exception/ErrorCode.java b/src/main/java/io/github/depromeet/knockknockbackend/global/error/exception/ErrorCode.java index b6ba08b3..7a68a42a 100644 --- a/src/main/java/io/github/depromeet/knockknockbackend/global/error/exception/ErrorCode.java +++ b/src/main/java/io/github/depromeet/knockknockbackend/global/error/exception/ErrorCode.java @@ -13,6 +13,8 @@ public enum ErrorCode { ARGUMENT_NOT_VALID_ERROR(HttpStatus.BAD_REQUEST.value(), "GLOBAL-400-1", "validation error"), EXPIRED_TOKEN(HttpStatus.UNAUTHORIZED.value(), "GLOBAL-401-1", "Expired Jwt Token."), + REGISTER_EXPIRED_TOKEN(HttpStatus.FORBIDDEN.value(), "GLOBAL-403-1", "refreshToken expired."), + INVALID_TOKEN(HttpStatus.UNAUTHORIZED.value(), "GLOBAL-401-1", "Invalid Jwt Token."), USER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "GLOBAL-404-1", "User Not Found."), From 6bf86306c99d36ac5807487b413afd359728df29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=8E=E1=85=A1=E1=86=AB=E1=84=8C?= =?UTF-8?q?=E1=85=B5=E1=86=AB?= Date: Fri, 30 Dec 2022 16:37:09 +0900 Subject: [PATCH 2/3] :recycle: :: [jwtProvider] getRefreshExp --- .../knockknockbackend/global/security/JwtTokenProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java b/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java index 7c99da1c..a0581c4a 100644 --- a/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java +++ b/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java @@ -104,7 +104,7 @@ public String generateAccessToken(Long id, AccountRole accountRole) { public String generateRefreshToken(Long id) { final Date issuedAt = new Date(); final Date refreshTokenExpiresIn = - new Date(issuedAt.getTime() + jwtProperties.getAccessExp() * 1000); + new Date(issuedAt.getTime() + jwtProperties.getRefreshExp() * 1000); return buildRefreshToken(id, issuedAt, refreshTokenExpiresIn); } From 4992b52951b95299736c0a2ffd7efe8c25b349a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=8E=E1=85=A1=E1=86=AB=E1=84=8C?= =?UTF-8?q?=E1=85=B5=E1=86=AB?= Date: Fri, 30 Dec 2022 17:03:40 +0900 Subject: [PATCH 3/3] =?UTF-8?q?:recycle:=20::=20[credential]=20redis=20?= =?UTF-8?q?=EB=A7=8C=EB=A3=8C=EC=8B=9C=EA=B0=84=20=EC=9E=AC=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../credential/service/CredentialService.java | 5 ++--- .../global/security/JwtTokenProvider.java | 15 ++++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java b/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java index d558fe91..3e75fa8e 100644 --- a/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java +++ b/src/main/java/io/github/depromeet/knockknockbackend/domain/credential/service/CredentialService.java @@ -17,7 +17,6 @@ import io.github.depromeet.knockknockbackend.global.exception.UserNotFoundException; import io.github.depromeet.knockknockbackend.global.security.JwtTokenProvider; import io.github.depromeet.knockknockbackend.global.utils.user.UserUtils; -import java.util.Date; import java.util.Optional; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; @@ -89,11 +88,11 @@ public AfterOauthResponse oauthCodeToUser(OauthProvider oauthProvider, String co // 레디스 ttl private String generateRefreshToken(Long userId) { String refreshToken = jwtTokenProvider.generateRefreshToken(userId); - Date tokenExpiredAt = jwtTokenProvider.getTokenExpiredAt(refreshToken); + Long tokenExpiredAt = jwtTokenProvider.getRefreshTokenTTlSecond(); RefreshTokenRedisEntity build = RefreshTokenRedisEntity.builder() .id(userId.toString()) - .ttl(tokenExpiredAt.getTime()) + .ttl(tokenExpiredAt) .refreshToken(refreshToken) .build(); refreshTokenRedisEntityRepository.save(build); diff --git a/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java b/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java index a0581c4a..18733ccd 100644 --- a/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java +++ b/src/main/java/io/github/depromeet/knockknockbackend/global/security/JwtTokenProvider.java @@ -1,6 +1,7 @@ package io.github.depromeet.knockknockbackend.global.security; +import io.github.depromeet.knockknockbackend.domain.credential.exception.RefreshTokenExpiredException; import io.github.depromeet.knockknockbackend.domain.user.domain.AccountRole; import io.github.depromeet.knockknockbackend.global.exception.ExpiredTokenException; import io.github.depromeet.knockknockbackend.global.exception.InvalidTokenException; @@ -125,14 +126,18 @@ public Long parseAccessToken(String token) { } public Long parseRefreshToken(String token) { - if (isRefreshToken(token)) { - Claims claims = getJws(token).getBody(); - return Long.parseLong(claims.getSubject()); + try { + if (isRefreshToken(token)) { + Claims claims = getJws(token).getBody(); + return Long.parseLong(claims.getSubject()); + } + } catch (ExpiredTokenException e) { + throw RefreshTokenExpiredException.EXCEPTION; } throw InvalidTokenException.EXCEPTION; } - public Date getTokenExpiredAt(String token) { - return getJws(token).getBody().getExpiration(); + public Long getRefreshTokenTTlSecond() { + return jwtProperties.getRefreshExp(); } }