From 3d1e4bbb4ffccbdb82984102ddd3dc68b79ed9dc Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 11:09:08 +0900 Subject: [PATCH 01/15] =?UTF-8?q?feat:=20=EC=A0=84=EC=97=AD=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=ED=95=B8=EB=93=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/gdschongik/gdsc/global/exception/GlobalExceptionHandler.java diff --git a/src/main/java/com/gdschongik/gdsc/global/exception/GlobalExceptionHandler.java b/src/main/java/com/gdschongik/gdsc/global/exception/GlobalExceptionHandler.java new file mode 100644 index 000000000..42cd4a1ed --- /dev/null +++ b/src/main/java/com/gdschongik/gdsc/global/exception/GlobalExceptionHandler.java @@ -0,0 +1,25 @@ +package com.gdschongik.gdsc.global.exception; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { + + @ExceptionHandler(CustomException.class) + public ResponseEntity handleCustomException(CustomException e) { + log.error("CustomException : {}", e.getMessage(), e); + return ResponseEntity.status(e.getErrorCode().getStatus()).body(ErrorResponse.of(e.getErrorCode())); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception e) { + log.error("INTERNAL_SERVER_ERROR : {}", e.getMessage(), e); + return ResponseEntity.status(ErrorCode.INTERNAL_SERVER_ERROR.getStatus()) + .body(ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR)); + } +} From cf4c105e4c456b31274ee119bb91f6dcd1c12ad8 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 11:09:43 +0900 Subject: [PATCH 02/15] =?UTF-8?q?feat:=20ErrorResponse=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gdschongik/gdsc/global/exception/ErrorResponse.java | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/com/gdschongik/gdsc/global/exception/ErrorResponse.java diff --git a/src/main/java/com/gdschongik/gdsc/global/exception/ErrorResponse.java b/src/main/java/com/gdschongik/gdsc/global/exception/ErrorResponse.java new file mode 100644 index 000000000..e818b1075 --- /dev/null +++ b/src/main/java/com/gdschongik/gdsc/global/exception/ErrorResponse.java @@ -0,0 +1,7 @@ +package com.gdschongik.gdsc.global.exception; + +public record ErrorResponse(String errorCodeName, String errorMessage) { + public static ErrorResponse of(ErrorCode errorCode) { + return new ErrorResponse(errorCode.name(), errorCode.getMessage()); + } +} From b24421b03fb3aa51ec218df091584a1516060913 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 11:10:08 +0900 Subject: [PATCH 03/15] =?UTF-8?q?feat:=20ErrorCode=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gdsc/global/exception/ErrorCode.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java diff --git a/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java b/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java new file mode 100644 index 000000000..f14e44c78 --- /dev/null +++ b/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java @@ -0,0 +1,14 @@ +package com.gdschongik.gdsc.global.exception; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +@AllArgsConstructor +public enum ErrorCode { + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러입니다."); + + private final HttpStatus status; + private final String message; +} From 0ef924e6de4d77760102cc3a5b5bbdf64c8410b1 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 11:10:27 +0900 Subject: [PATCH 04/15] =?UTF-8?q?feat:=20CustomException=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gdsc/global/exception/CustomException.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/main/java/com/gdschongik/gdsc/global/exception/CustomException.java diff --git a/src/main/java/com/gdschongik/gdsc/global/exception/CustomException.java b/src/main/java/com/gdschongik/gdsc/global/exception/CustomException.java new file mode 100644 index 000000000..fc1b0f5a1 --- /dev/null +++ b/src/main/java/com/gdschongik/gdsc/global/exception/CustomException.java @@ -0,0 +1,14 @@ +package com.gdschongik.gdsc.global.exception; + +import lombok.Getter; + +@Getter +public class CustomException extends RuntimeException { + + private final ErrorCode errorCode; + + public CustomException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } +} From c120e8bbbe88ad869417c2d73ea98d2207871e6b Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 15:59:10 +0900 Subject: [PATCH 05/15] =?UTF-8?q?feat:=20JwtFilter=EC=9D=98=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=ED=95=84=ED=84=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gdschongik/gdsc/global/config/WebSecurityConfig.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/com/gdschongik/gdsc/global/config/WebSecurityConfig.java b/src/main/java/com/gdschongik/gdsc/global/config/WebSecurityConfig.java index d6dea291b..a1e47e596 100644 --- a/src/main/java/com/gdschongik/gdsc/global/config/WebSecurityConfig.java +++ b/src/main/java/com/gdschongik/gdsc/global/config/WebSecurityConfig.java @@ -2,10 +2,12 @@ import static org.springframework.security.config.Customizer.*; +import com.fasterxml.jackson.databind.ObjectMapper; import com.gdschongik.gdsc.domain.auth.application.JwtService; import com.gdschongik.gdsc.domain.member.dao.MemberRepository; import com.gdschongik.gdsc.global.security.CustomSuccessHandler; import com.gdschongik.gdsc.global.security.CustomUserService; +import com.gdschongik.gdsc.global.security.JwtExceptionFilter; import com.gdschongik.gdsc.global.security.JwtFilter; import com.gdschongik.gdsc.global.util.CookieUtil; import lombok.RequiredArgsConstructor; @@ -26,6 +28,7 @@ public class WebSecurityConfig { private final MemberRepository memberRepository; private final JwtService jwtService; private final CookieUtil cookieUtil; + private final ObjectMapper objectMapper; @Bean public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception { @@ -41,6 +44,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti .successHandler(customSuccessHandler(jwtService, cookieUtil))); httpSecurity.addFilterBefore(jwtFilter(jwtService, cookieUtil), UsernamePasswordAuthenticationFilter.class); + httpSecurity.addFilterBefore(jwtExceptionFilter(objectMapper), JwtFilter.class); return httpSecurity.build(); } @@ -59,4 +63,9 @@ public CustomSuccessHandler customSuccessHandler(JwtService jwtService, CookieUt public JwtFilter jwtFilter(JwtService jwtService, CookieUtil cookieUtil) { return new JwtFilter(jwtService, cookieUtil); } + + @Bean + public JwtExceptionFilter jwtExceptionFilter(ObjectMapper objectMapper) { + return new JwtExceptionFilter(objectMapper); + } } From 496e10be4f7f0ed31e2d8b2f3c4e859a8be67cf2 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:00:33 +0900 Subject: [PATCH 06/15] =?UTF-8?q?refactor:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=ED=8C=8C=EC=8B=B1=20=EA=B3=BC=EC=A0=95=EC=9D=98=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EB=A5=BC=20CustomException=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java index bc8dacdc6..b08127102 100644 --- a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java +++ b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java @@ -6,6 +6,8 @@ import com.gdschongik.gdsc.domain.auth.dto.RefreshTokenDto; import com.gdschongik.gdsc.domain.member.domain.MemberRole; import com.gdschongik.gdsc.global.common.constant.JwtConstant; +import com.gdschongik.gdsc.global.exception.CustomException; +import com.gdschongik.gdsc.global.exception.ErrorCode; import com.gdschongik.gdsc.global.property.JwtProperty; import io.jsonwebtoken.Claims; import io.jsonwebtoken.ExpiredJwtException; @@ -81,9 +83,9 @@ public AccessTokenDto parseAccessToken(String accessTokenValue) throws ExpiredJw MemberRole.valueOf(claims.getBody().get(TOKEN_ROLE_NAME, String.class)), accessTokenValue); } catch (ExpiredJwtException e) { - throw e; + throw new CustomException(ErrorCode.Expired_JWT_TOKEN); } catch (Exception e) { - return null; + throw new CustomException(ErrorCode.INVALID_JWT_TOKEN); } } From 464f99b68ebe9441ee6aeda2da116c104a8b6bc2 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:08:48 +0900 Subject: [PATCH 07/15] =?UTF-8?q?remove:=20CustomException=20=EB=8D=98?= =?UTF-8?q?=EC=A7=80=EB=AF=80=EB=A1=9C=20throws=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java index b08127102..cbcc85837 100644 --- a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java +++ b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java @@ -74,7 +74,7 @@ private Key getKey(JwtConstant jwtConstant) { jwtProperty.getToken().get(jwtConstant).secret().getBytes()); } - public AccessTokenDto parseAccessToken(String accessTokenValue) throws ExpiredJwtException { + public AccessTokenDto parseAccessToken(String accessTokenValue) { try { Jws claims = getClaims(JwtConstant.ACCESS_TOKEN, accessTokenValue); From f9b56a7d780d4232cd0a9b601f0f1260ccd53d8a Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:09:58 +0900 Subject: [PATCH 08/15] =?UTF-8?q?refactor:=20parseAccessToken=20=EB=82=B4?= =?UTF-8?q?=EB=B6=80=EC=97=90=EC=84=9C=20CustomException=20=EB=8D=98?= =?UTF-8?q?=EC=A7=80=EB=AF=80=EB=A1=9C=20try-catch=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gdschongik/gdsc/domain/auth/application/JwtService.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java b/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java index c5877110a..2d5a3e05b 100644 --- a/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java +++ b/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java @@ -42,11 +42,7 @@ private void saveRefreshTokenToRedis(RefreshTokenDto refreshTokenDto) { } public AccessTokenDto retrieveAccessToken(String accessTokenValue) { - try { - return jwtUtil.parseAccessToken(accessTokenValue); - } catch (Exception e) { - return null; - } + return jwtUtil.parseAccessToken(accessTokenValue); } public RefreshTokenDto retrieveRefreshToken(String refreshTokenValue) { From 704be5a3fc81fec6bbf19dd6f5a7aba6434b7e8c Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:11:37 +0900 Subject: [PATCH 09/15] =?UTF-8?q?typo:=20=EC=98=A4=ED=83=80=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 --- src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java index cbcc85837..421f325e6 100644 --- a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java +++ b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java @@ -83,7 +83,7 @@ public AccessTokenDto parseAccessToken(String accessTokenValue) { MemberRole.valueOf(claims.getBody().get(TOKEN_ROLE_NAME, String.class)), accessTokenValue); } catch (ExpiredJwtException e) { - throw new CustomException(ErrorCode.Expired_JWT_TOKEN); + throw new CustomException(ErrorCode.EXPIRED_JWT_TOKEN); } catch (Exception e) { throw new CustomException(ErrorCode.INVALID_JWT_TOKEN); } From 148dd724693e2488c787b8ee37245afe90b048a1 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:12:22 +0900 Subject: [PATCH 10/15] =?UTF-8?q?feat:=20Jwt=20=EA=B4=80=EB=A0=A8=20ErrorC?= =?UTF-8?q?ode=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/gdschongik/gdsc/global/exception/ErrorCode.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java b/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java index f14e44c78..a9ff9e2ab 100644 --- a/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java +++ b/src/main/java/com/gdschongik/gdsc/global/exception/ErrorCode.java @@ -7,7 +7,11 @@ @Getter @AllArgsConstructor public enum ErrorCode { - INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러입니다."); + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 에러입니다."), + + // Jwt + INVALID_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 JWT 토큰입니다."), + EXPIRED_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "만료된 JWT 토큰입니다."); private final HttpStatus status; private final String message; From 8cc12651f8b9b9672b8c6e8e304c9d27f9c9a035 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:12:59 +0900 Subject: [PATCH 11/15] =?UTF-8?q?feat:=20JwtFilter=EC=9D=98=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=ED=95=84=ED=84=B0=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/security/JwtExceptionFilter.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/com/gdschongik/gdsc/global/security/JwtExceptionFilter.java diff --git a/src/main/java/com/gdschongik/gdsc/global/security/JwtExceptionFilter.java b/src/main/java/com/gdschongik/gdsc/global/security/JwtExceptionFilter.java new file mode 100644 index 000000000..c5d9028c0 --- /dev/null +++ b/src/main/java/com/gdschongik/gdsc/global/security/JwtExceptionFilter.java @@ -0,0 +1,34 @@ +package com.gdschongik.gdsc.global.security; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.gdschongik.gdsc.global.exception.CustomException; +import com.gdschongik.gdsc.global.exception.ErrorResponse; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.filter.OncePerRequestFilter; + +@Slf4j +@RequiredArgsConstructor +public class JwtExceptionFilter extends OncePerRequestFilter { + + private final ObjectMapper objectMapper; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) + throws ServletException, IOException { + try { + filterChain.doFilter(request, response); + } catch (CustomException e) { + log.error("JWTException : {}", e.getMessage(), e); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + response.setStatus(e.getErrorCode().getStatus().value()); + response.getWriter().write(objectMapper.writeValueAsString(ErrorResponse.of(e.getErrorCode()))); + } + } +} From 3cdf2d6f593d8f5c34efb7a7c85ae63b1b450376 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 16:38:31 +0900 Subject: [PATCH 12/15] =?UTF-8?q?fix:=20=EC=9E=AC=EB=B0=9C=EA=B8=89=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=EC=97=90=EC=84=9C=20=EC=9E=A1?= =?UTF-8?q?=EC=9D=84=20=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=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 --- src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java index 421f325e6..de557dfdd 100644 --- a/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java +++ b/src/main/java/com/gdschongik/gdsc/global/util/JwtUtil.java @@ -74,7 +74,7 @@ private Key getKey(JwtConstant jwtConstant) { jwtProperty.getToken().get(jwtConstant).secret().getBytes()); } - public AccessTokenDto parseAccessToken(String accessTokenValue) { + public AccessTokenDto parseAccessToken(String accessTokenValue) throws ExpiredJwtException { try { Jws claims = getClaims(JwtConstant.ACCESS_TOKEN, accessTokenValue); @@ -83,7 +83,7 @@ public AccessTokenDto parseAccessToken(String accessTokenValue) { MemberRole.valueOf(claims.getBody().get(TOKEN_ROLE_NAME, String.class)), accessTokenValue); } catch (ExpiredJwtException e) { - throw new CustomException(ErrorCode.EXPIRED_JWT_TOKEN); + throw e; } catch (Exception e) { throw new CustomException(ErrorCode.INVALID_JWT_TOKEN); } From 19fb0172a873ed19bbd776edbf7a1ef2a98826ae Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Tue, 30 Jan 2024 22:39:30 +0900 Subject: [PATCH 13/15] =?UTF-8?q?fix:=20try-catch=EB=A1=9C=20=EB=A7=8C?= =?UTF-8?q?=EB=A3=8C=20=ED=86=A0=ED=81=B0=EC=9D=98=20=EA=B2=BD=EC=9A=B0=20?= =?UTF-8?q?null=EC=9D=84=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gdschongik/gdsc/domain/auth/application/JwtService.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java b/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java index 2d5a3e05b..458b43db6 100644 --- a/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java +++ b/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java @@ -42,7 +42,11 @@ private void saveRefreshTokenToRedis(RefreshTokenDto refreshTokenDto) { } public AccessTokenDto retrieveAccessToken(String accessTokenValue) { - return jwtUtil.parseAccessToken(accessTokenValue); + try { + return jwtUtil.parseAccessToken(accessTokenValue); + } catch (ExpiredJwtException e) { + return null; + } } public RefreshTokenDto retrieveRefreshToken(String refreshTokenValue) { From ca825796cf7e4f91bb909ee81a54226b8de4ba0e Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Wed, 31 Jan 2024 13:52:58 +0900 Subject: [PATCH 14/15] =?UTF-8?q?fix:=20=ED=86=A0=ED=81=B0=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존 메서드는 데이터의 유무와 무관하게 항상 비어있는 Optional을 반환함. 사용하지 않는 Repository의 메서드는 제거함. --- .../gdschongik/gdsc/domain/auth/application/JwtService.java | 2 +- .../gdsc/domain/auth/dao/RefreshTokenRepository.java | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java b/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java index 458b43db6..73811cbe9 100644 --- a/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java +++ b/src/main/java/com/gdschongik/gdsc/domain/auth/application/JwtService.java @@ -71,7 +71,7 @@ public RefreshTokenDto retrieveRefreshToken(String refreshTokenValue) { private Optional getRefreshTokenFromRedis(Long memberId) { // TODO: CustomException으로 바꾸기 - return refreshTokenRepository.findByMemberId(memberId); + return refreshTokenRepository.findById(memberId); } private RefreshTokenDto parseRefreshToken(String refreshTokenValue) { diff --git a/src/main/java/com/gdschongik/gdsc/domain/auth/dao/RefreshTokenRepository.java b/src/main/java/com/gdschongik/gdsc/domain/auth/dao/RefreshTokenRepository.java index 0d81979e7..53d4bae5f 100644 --- a/src/main/java/com/gdschongik/gdsc/domain/auth/dao/RefreshTokenRepository.java +++ b/src/main/java/com/gdschongik/gdsc/domain/auth/dao/RefreshTokenRepository.java @@ -1,9 +1,6 @@ package com.gdschongik.gdsc.domain.auth.dao; import com.gdschongik.gdsc.domain.auth.domain.RefreshToken; -import java.util.Optional; import org.springframework.data.repository.CrudRepository; -public interface RefreshTokenRepository extends CrudRepository { - Optional findByMemberId(Long aLong); -} +public interface RefreshTokenRepository extends CrudRepository {} From 9c634488a1c77509fcfa9ffb05b9d1ca2fa0fb03 Mon Sep 17 00:00:00 2001 From: "chosw1002@naver.com" Date: Wed, 31 Jan 2024 13:54:13 +0900 Subject: [PATCH 15/15] =?UTF-8?q?fix:=20refreshToken=EC=9D=84=20accessToke?= =?UTF-8?q?n=EC=9C=BC=EB=A1=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit accessToken을 받아야 하는 메서드인데 refreshToken을 받고 있었음. --- .../java/com/gdschongik/gdsc/global/security/JwtFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/gdschongik/gdsc/global/security/JwtFilter.java b/src/main/java/com/gdschongik/gdsc/global/security/JwtFilter.java index 011dc9ee6..355ab70e9 100644 --- a/src/main/java/com/gdschongik/gdsc/global/security/JwtFilter.java +++ b/src/main/java/com/gdschongik/gdsc/global/security/JwtFilter.java @@ -50,7 +50,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse } Optional reissueAccessToken = - Optional.ofNullable(jwtService.reissueAccessTokenIfExpired(refreshTokenValue)); + Optional.ofNullable(jwtService.reissueAccessTokenIfExpired(accessTokenValue)); RefreshTokenDto refreshTokenDto = jwtService.retrieveRefreshToken(refreshTokenValue); // AT가 만료되었고, RT가 유효하면 AT, RT 재발급