From d688dc7bb60a4989c643476df7f41a6b077fd7c0 Mon Sep 17 00:00:00 2001 From: heejjinkim Date: Sat, 19 Oct 2024 00:16:39 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=9E=90=EB=8F=99=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=EC=9D=84=20=EC=9C=84=ED=95=B4=20access=20token=20?= =?UTF-8?q?=EB=A7=8C=EB=A3=8C=20=ED=99=95=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit related to: #11 --- .../wepro/global/config/SwaggerConfig.java | 5 ++-- .../wepro/member/service/ReissueService.java | 25 ++++++------------- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/_119/wepro/global/config/SwaggerConfig.java b/src/main/java/com/_119/wepro/global/config/SwaggerConfig.java index 85c2522..b34ba50 100644 --- a/src/main/java/com/_119/wepro/global/config/SwaggerConfig.java +++ b/src/main/java/com/_119/wepro/global/config/SwaggerConfig.java @@ -1,5 +1,7 @@ package com._119.wepro.global.config; +import static com._119.wepro.global.security.constant.SecurityConstants.REFRESH_TOKEN_HEADER; + import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; @@ -8,7 +10,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpHeaders; -import org.springframework.security.oauth2.core.AuthorizationGrantType; @Configuration public class SwaggerConfig { @@ -48,7 +49,7 @@ private SecurityScheme createRefreshTokenSecurityScheme() { return new SecurityScheme() .type(SecurityScheme.Type.APIKEY) .in(SecurityScheme.In.HEADER) - .name(AuthorizationGrantType.REFRESH_TOKEN.getValue()); + .name(REFRESH_TOKEN_HEADER); } private Info createApiInfo() { diff --git a/src/main/java/com/_119/wepro/member/service/ReissueService.java b/src/main/java/com/_119/wepro/member/service/ReissueService.java index e73c2b0..90def13 100644 --- a/src/main/java/com/_119/wepro/member/service/ReissueService.java +++ b/src/main/java/com/_119/wepro/member/service/ReissueService.java @@ -17,8 +17,11 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; + +@Slf4j @Service @RequiredArgsConstructor public class ReissueService { @@ -31,15 +34,14 @@ public void reissue(HttpServletRequest request, HttpServletResponse response) { String refreshToken = request.getHeader(REFRESH_TOKEN_HEADER); String accessToken = extractToken(request.getHeader(ACCESS_TOKEN_HEADER)); - validateAccessTokenExpired(accessToken); - String providerId = jwtTokenProvider.parseExpiredToken(accessToken).getSubject(); + String memberId = jwtTokenProvider.parseExpiredToken(accessToken).getSubject(); - validateRefreshToken(refreshToken, providerId); + validateRefreshToken(refreshToken, memberId); - Member member = memberRepository.findByProviderId(providerId) + Member member = memberRepository.findById(Long.parseLong(memberId)) .orElseThrow(() -> new RestApiException(UserErrorCode.USER_NOT_FOUND)); - TokenInfo newTokenInfo = jwtTokenProvider.generateToken(providerId, member.getRole()); + TokenInfo newTokenInfo = jwtTokenProvider.generateToken(memberId, member.getRole()); setTokenPairToResponseHeader(response, newTokenInfo.getAccessToken(), newTokenInfo.getRefreshToken()); } @@ -52,17 +54,6 @@ private String extractToken(String token) { return token.replace(GRANT_TYPE, ""); } - private void validateAccessTokenExpired(String accessToken) { - try { - jwtTokenProvider.validateToken(accessToken); - throw new RestApiException(REFRESH_DENIED); - } catch (RestApiException e) { - if (e.getErrorCode() != EXPIRED_TOKEN) { - throw e; - } - } - } - private void validateRefreshToken(String refreshToken, String memberId) { String savedRefreshToken = jwtTokenProvider.getRefreshToken(memberId); if (!refreshToken.equals(savedRefreshToken)) { @@ -73,6 +64,6 @@ private void validateRefreshToken(String refreshToken, String memberId) { private void setTokenPairToResponseHeader( HttpServletResponse response, String accessToken, String refreshToken) { response.setHeader(ACCESS_TOKEN_HEADER, GRANT_TYPE + accessToken); - response.setHeader(REFRESH_TOKEN_HEADER, GRANT_TYPE + refreshToken); + response.setHeader(REFRESH_TOKEN_HEADER, refreshToken); } }