From aff1104ceda69a25697f78018ade1cba9d8452ea Mon Sep 17 00:00:00 2001 From: ckkim817 Date: Sun, 2 Jun 2024 23:59:03 +0900 Subject: [PATCH 1/4] =?UTF-8?q?6=EC=B0=A8=20=EC=84=B8=EB=AF=B8=EB=82=98=20?= =?UTF-8?q?=EC=8B=A4=EC=8A=B5=20=EA=B3=BC=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/redis/config/RedisConfig.java | 19 +++++++++ .../ReissueAccessTokenController.java | 30 ++++++++++++++ .../service/ReissueAccessTokenService.java | 39 +++++++++++++++++++ .../redis/service/dto/AccessTokenDTO.java | 9 +++++ .../common/dto/ErrorMessage.java | 3 +- .../common/jwt/JwtTokenProvider.java | 13 ++++++- .../springPractice/service/MemberService.java | 9 ++++- .../service/dto/UserJoinResponse.java | 4 +- 8 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java create mode 100644 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java create mode 100644 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java create mode 100644 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/dto/AccessTokenDTO.java diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java new file mode 100644 index 0000000..c530d43 --- /dev/null +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java @@ -0,0 +1,19 @@ +package org.sopt.springPractice.auth.redis.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +public class RedisConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + return template; + } +} diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java new file mode 100644 index 0000000..ba7532f --- /dev/null +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java @@ -0,0 +1,30 @@ +package org.sopt.springPractice.auth.redis.controller; + +import lombok.RequiredArgsConstructor; +import org.sopt.springPractice.auth.PrincipalHandler; +import org.sopt.springPractice.auth.redis.service.dto.AccessTokenDTO; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Transactional +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/member") +public class ReissueAccessTokenController { + + private final org.sopt.springPractice.auth.redis.service.ReissueAccessTokenService reissueAccessTokenService; + private final PrincipalHandler principalHandler; + + @PostMapping("/reissue-Token") + public ResponseEntity reissueAccessToken() { + Long userId = principalHandler.getUserIdFromPrincipal(); + AccessTokenDTO newAccessTokenResponse = reissueAccessTokenService.reissueAccessToken(userId); + + return ResponseEntity.status(HttpStatus.CREATED) + .body(newAccessTokenResponse); + } +} diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java new file mode 100644 index 0000000..d9d02d9 --- /dev/null +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java @@ -0,0 +1,39 @@ +package org.sopt.springPractice.auth.redis.service; + +import lombok.RequiredArgsConstructor; +import org.sopt.springPractice.auth.redis.domain.Token; +import org.sopt.springPractice.auth.redis.repository.RedisTokenRepository; +import org.sopt.springPractice.auth.redis.service.dto.AccessTokenDTO; +import org.sopt.springPractice.common.dto.ErrorMessage; +import org.sopt.springPractice.common.jwt.JwtTokenProvider; +import org.sopt.springPractice.common.jwt.JwtValidationType; +import org.sopt.springPractice.exception.UnauthorizedException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class ReissueAccessTokenService { + + private final RedisTokenRepository redisTokenRepository; + private final JwtTokenProvider jwtTokenProvider; + + @Transactional + public AccessTokenDTO reissueAccessToken(Long userId) { + Token token = redisTokenRepository.findById(userId).orElseThrow( + () -> new UnauthorizedException(ErrorMessage.REFRESH_TOKEN_NOT_FOUND) + ); + + JwtValidationType validationType = jwtTokenProvider.validateToken(token.getRefreshToken()); + + if (validationType == JwtValidationType.EXPIRED_JWT_TOKEN) { + throw new UnauthorizedException(ErrorMessage.JWT_UNAUTHORIZED_EXCEPTION); + } else if (validationType != JwtValidationType.VALID_JWT) { + throw new UnauthorizedException(ErrorMessage.JWT_UNAUTHORIZED_EXCEPTION); + } + + String newAccessToken = jwtTokenProvider.newAccessToken(token.getRefreshToken()); + + return AccessTokenDTO.of(newAccessToken); + } +} diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/dto/AccessTokenDTO.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/dto/AccessTokenDTO.java new file mode 100644 index 0000000..3a46ade --- /dev/null +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/dto/AccessTokenDTO.java @@ -0,0 +1,9 @@ +package org.sopt.springPractice.auth.redis.service.dto; + +public record AccessTokenDTO( + String accessToken +) { + public static AccessTokenDTO of(String accessToken) { + return new AccessTokenDTO(accessToken); + } +} diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java index a3db62d..032447c 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java @@ -10,7 +10,8 @@ public enum ErrorMessage { MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 사용자가 존재하지 않습니다."), BLOG_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 블로그가 존재하지 않습니다."), - JWT_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED.value(), "사용자의 로그인 검증을 실패했습니다."); + JWT_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED.value(), "사용자의 로그인 검증을 실패했습니다."), + REFRESH_TOKEN_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "refresh token이 존재하지 않습니다."); private final int status; private final String message; } diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/jwt/JwtTokenProvider.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/jwt/JwtTokenProvider.java index 4d02f84..647e007 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/jwt/JwtTokenProvider.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/jwt/JwtTokenProvider.java @@ -12,6 +12,7 @@ import java.util.Date; import javax.crypto.SecretKey; import lombok.RequiredArgsConstructor; +import org.sopt.springPractice.auth.UserAuthentication; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.stereotype.Component; @@ -22,8 +23,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 REFRESH_TOKEN_EXPIRATION_TIME = 60 * 60 * 24 * 1000L * 14; + private static final Long ACCESS_TOKEN_EXPIRATION_TIME = 24 * 60 * 60 * 1000L * 2; + private static final Long REFRESH_TOKEN_EXPIRATION_TIME = 24 * 60 * 60 * 1000L * 14; @Value("${jwt.secret}") private String JWT_SECRET; @@ -83,4 +84,12 @@ public Long getUserFromJwt(String token) { Claims claims = getBody(token); return Long.valueOf(claims.get(USER_ID).toString()); } + + public String newAccessToken(String refreshToken) { + Claims claims = getBody(refreshToken); + Long userId = Long.valueOf(claims.get(USER_ID).toString()); + Authentication authentication = UserAuthentication.createUserAuthentication(userId); + + return issueAccessToken(authentication); + } } diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/MemberService.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/MemberService.java index d7d6ee6..09ff650 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/MemberService.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/MemberService.java @@ -3,6 +3,8 @@ import jakarta.persistence.EntityNotFoundException; import lombok.RequiredArgsConstructor; import org.sopt.springPractice.auth.UserAuthentication; +import org.sopt.springPractice.auth.redis.domain.Token; +import org.sopt.springPractice.auth.redis.repository.RedisTokenRepository; import org.sopt.springPractice.common.dto.ErrorMessage; import org.sopt.springPractice.common.jwt.JwtTokenProvider; import org.sopt.springPractice.domain.Member; @@ -21,6 +23,7 @@ public class MemberService { private final MemberRepository memberRepository; private final JwtTokenProvider jwtTokenProvider; + private final RedisTokenRepository redisTokenRepository; @Transactional public UserJoinResponse createMember( @@ -33,8 +36,12 @@ public UserJoinResponse createMember( String accessToken = jwtTokenProvider.issueAccessToken( UserAuthentication.createUserAuthentication(memberId) ); + String refreshToken = jwtTokenProvider.issueRefreshToken( + UserAuthentication.createUserAuthentication(memberId) + ); + redisTokenRepository.save(Token.of(memberId, refreshToken)); - return UserJoinResponse.of(accessToken, memberId.toString()); + return UserJoinResponse.of(accessToken, refreshToken, memberId.toString()); } public Member findById( diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/dto/UserJoinResponse.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/dto/UserJoinResponse.java index 6835016..bcecca2 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/dto/UserJoinResponse.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/service/dto/UserJoinResponse.java @@ -2,12 +2,14 @@ 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); } } From 746ab613b746b4498be10a2629bc556ee2d9859b Mon Sep 17 00:00:00 2001 From: ckkim817 Date: Mon, 3 Jun 2024 01:37:07 +0900 Subject: [PATCH 2/4] =?UTF-8?q?6=EC=B0=A8=20=EC=84=B8=EB=AF=B8=EB=82=98=20?= =?UTF-8?q?=EC=8B=A4=EC=8A=B5=20=EA=B3=BC=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/redis/config/RedisConfig.java | 16 ++++++++++++++++ ...TokenController.java => TokenController.java} | 11 ++++++----- ...AccessTokenService.java => TokenService.java} | 7 ++++++- 3 files changed, 28 insertions(+), 6 deletions(-) rename 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/{ReissueAccessTokenController.java => TokenController.java} (73%) rename 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/{ReissueAccessTokenService.java => TokenService.java} (94%) diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java index c530d43..caf42b4 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/config/RedisConfig.java @@ -1,19 +1,35 @@ package org.sopt.springPractice.auth.redis.config; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; +@Configuration public class RedisConfig { + @Value("${spring.data.redis.host}") + private String host; + + @Value("${spring.data.redis.port}") + private int port; + + @Bean + public RedisConnectionFactory redisConnectionFactory() { + return new LettuceConnectionFactory(host, port); + } + @Bean public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate template = new RedisTemplate<>(); template.setConnectionFactory(connectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); + return template; } } diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/TokenController.java similarity index 73% rename from 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java rename to 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/TokenController.java index ba7532f..1880ed8 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/ReissueAccessTokenController.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/controller/TokenController.java @@ -2,6 +2,7 @@ import lombok.RequiredArgsConstructor; import org.sopt.springPractice.auth.PrincipalHandler; +import org.sopt.springPractice.auth.redis.service.TokenService; import org.sopt.springPractice.auth.redis.service.dto.AccessTokenDTO; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -13,16 +14,16 @@ @Transactional @RestController @RequiredArgsConstructor -@RequestMapping("/api/v1/member") -public class ReissueAccessTokenController { +@RequestMapping("/api/v1/token") +public class TokenController { - private final org.sopt.springPractice.auth.redis.service.ReissueAccessTokenService reissueAccessTokenService; + private final TokenService tokenService; private final PrincipalHandler principalHandler; - @PostMapping("/reissue-Token") + @PostMapping("/reissue") public ResponseEntity reissueAccessToken() { Long userId = principalHandler.getUserIdFromPrincipal(); - AccessTokenDTO newAccessTokenResponse = reissueAccessTokenService.reissueAccessToken(userId); + AccessTokenDTO newAccessTokenResponse = tokenService.reissueAccessToken(userId); return ResponseEntity.status(HttpStatus.CREATED) .body(newAccessTokenResponse); diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java similarity index 94% rename from 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java rename to 6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java index d9d02d9..d2d3ccb 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/ReissueAccessTokenService.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java @@ -1,6 +1,7 @@ package org.sopt.springPractice.auth.redis.service; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.sopt.springPractice.auth.redis.domain.Token; import org.sopt.springPractice.auth.redis.repository.RedisTokenRepository; import org.sopt.springPractice.auth.redis.service.dto.AccessTokenDTO; @@ -13,13 +14,17 @@ @Service @RequiredArgsConstructor -public class ReissueAccessTokenService { +@Slf4j +public class TokenService { private final RedisTokenRepository redisTokenRepository; private final JwtTokenProvider jwtTokenProvider; @Transactional public AccessTokenDTO reissueAccessToken(Long userId) { + + log.warn("{}", userId); + Token token = redisTokenRepository.findById(userId).orElseThrow( () -> new UnauthorizedException(ErrorMessage.REFRESH_TOKEN_NOT_FOUND) ); From f7b1a3b99b9fb85807395976968c0d55d91c72ae Mon Sep 17 00:00:00 2001 From: ckkim817 Date: Mon, 3 Jun 2024 02:17:17 +0900 Subject: [PATCH 3/4] =?UTF-8?q?MappingException=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/sopt/springPractice/auth/redis/domain/Token.java | 2 ++ .../sopt/springPractice/auth/redis/service/TokenService.java | 5 ----- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/domain/Token.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/domain/Token.java index 9759e2a..516ca35 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/domain/Token.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/domain/Token.java @@ -4,11 +4,13 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import org.springframework.data.redis.core.RedisHash; import org.springframework.data.redis.core.index.Indexed; @RedisHash(value = "", timeToLive = 60 * 60 * 24 * 1000L * 14) @AllArgsConstructor +@NoArgsConstructor @Getter @Builder public class Token { diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java index d2d3ccb..eb739ee 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java @@ -1,7 +1,6 @@ package org.sopt.springPractice.auth.redis.service; import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; import org.sopt.springPractice.auth.redis.domain.Token; import org.sopt.springPractice.auth.redis.repository.RedisTokenRepository; import org.sopt.springPractice.auth.redis.service.dto.AccessTokenDTO; @@ -14,7 +13,6 @@ @Service @RequiredArgsConstructor -@Slf4j public class TokenService { private final RedisTokenRepository redisTokenRepository; @@ -22,9 +20,6 @@ public class TokenService { @Transactional public AccessTokenDTO reissueAccessToken(Long userId) { - - log.warn("{}", userId); - Token token = redisTokenRepository.findById(userId).orElseThrow( () -> new UnauthorizedException(ErrorMessage.REFRESH_TOKEN_NOT_FOUND) ); From 8806e9fc4ec655fc22437c76cd9afaf31897bcf0 Mon Sep 17 00:00:00 2001 From: ckkim817 Date: Mon, 3 Jun 2024 03:10:40 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Refresh=20Token=20=EB=A7=8C=EB=A3=8C=20?= =?UTF-8?q?=EC=8B=9C=20ErrorMessage=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/redis/service/TokenService.java | 12 ++---------- .../sopt/springPractice/common/dto/ErrorMessage.java | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java index eb739ee..fe3f5e5 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/auth/redis/service/TokenService.java @@ -6,7 +6,6 @@ import org.sopt.springPractice.auth.redis.service.dto.AccessTokenDTO; import org.sopt.springPractice.common.dto.ErrorMessage; import org.sopt.springPractice.common.jwt.JwtTokenProvider; -import org.sopt.springPractice.common.jwt.JwtValidationType; import org.sopt.springPractice.exception.UnauthorizedException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -21,17 +20,10 @@ public class TokenService { @Transactional public AccessTokenDTO reissueAccessToken(Long userId) { Token token = redisTokenRepository.findById(userId).orElseThrow( - () -> new UnauthorizedException(ErrorMessage.REFRESH_TOKEN_NOT_FOUND) + () -> new UnauthorizedException(ErrorMessage.MEMBER_NOT_FOUND) ); - JwtValidationType validationType = jwtTokenProvider.validateToken(token.getRefreshToken()); - - if (validationType == JwtValidationType.EXPIRED_JWT_TOKEN) { - throw new UnauthorizedException(ErrorMessage.JWT_UNAUTHORIZED_EXCEPTION); - } else if (validationType != JwtValidationType.VALID_JWT) { - throw new UnauthorizedException(ErrorMessage.JWT_UNAUTHORIZED_EXCEPTION); - } - + jwtTokenProvider.validateToken(token.getRefreshToken()); String newAccessToken = jwtTokenProvider.newAccessToken(token.getRefreshToken()); return AccessTokenDTO.of(newAccessToken); diff --git a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java index 032447c..a3989ec 100644 --- a/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java +++ b/6thSeminar/springPractice/src/main/java/org/sopt/springPractice/common/dto/ErrorMessage.java @@ -11,7 +11,7 @@ public enum ErrorMessage { MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 사용자가 존재하지 않습니다."), BLOG_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 블로그가 존재하지 않습니다."), JWT_UNAUTHORIZED_EXCEPTION(HttpStatus.UNAUTHORIZED.value(), "사용자의 로그인 검증을 실패했습니다."), - REFRESH_TOKEN_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "refresh token이 존재하지 않습니다."); + EXPIRED_JWT_TOKEN(HttpStatus.UNAUTHORIZED.value(), "만료된 refresh 토큰입니다."); private final int status; private final String message; }