From 8811977e077cee31dd8d63286669ded7a0dc4036 Mon Sep 17 00:00:00 2001 From: CHAE Date: Thu, 25 Jan 2024 15:15:55 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Fix:=20Jwt=20=EC=9D=BC=EB=B6=80=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EB=B3=B5=EA=B5=AC=20=EB=B0=8F=20RefreshTo?= =?UTF-8?q?ken=20=EB=A7=8C=EB=A3=8C=20=EA=B4=80=EB=A0=A8=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EB=B0=98=EC=98=81=20(#152)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diareat/diareat/auth/component/JwtTokenProvider.java | 4 ++-- .../diareat/diareat/auth/controller/AuthController.java | 7 +++++++ .../diareat/util/exception/GlobalExceptionHandler.java | 7 +++++++ .../diareat/diareat/util/exception/ValidException.java | 9 +++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/diareat/diareat/util/exception/ValidException.java diff --git a/src/main/java/com/diareat/diareat/auth/component/JwtTokenProvider.java b/src/main/java/com/diareat/diareat/auth/component/JwtTokenProvider.java index eaf4ddf..a446dcf 100644 --- a/src/main/java/com/diareat/diareat/auth/component/JwtTokenProvider.java +++ b/src/main/java/com/diareat/diareat/auth/component/JwtTokenProvider.java @@ -1,7 +1,7 @@ package com.diareat.diareat.auth.component; import com.diareat.diareat.util.api.ResponseCode; -import com.diareat.diareat.util.exception.BaseException; +import com.diareat.diareat.util.exception.ValidException; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; @@ -94,7 +94,7 @@ public boolean validateAccessToken(String jwtToken) { public void validateRefreshToken(Long userPK, String refreshToken) { String redisRefreshToken = redisTemplate.opsForValue().get(String.valueOf(userPK)); if (redisRefreshToken == null || !redisRefreshToken.equals(refreshToken)) { - throw new BaseException(ResponseCode.REFRESH_TOKEN_VALIDATION_FAILURE); + throw new ValidException(ResponseCode.REFRESH_TOKEN_VALIDATION_FAILURE); } } diff --git a/src/main/java/com/diareat/diareat/auth/controller/AuthController.java b/src/main/java/com/diareat/diareat/auth/controller/AuthController.java index bd1396c..fb2f744 100644 --- a/src/main/java/com/diareat/diareat/auth/controller/AuthController.java +++ b/src/main/java/com/diareat/diareat/auth/controller/AuthController.java @@ -54,6 +54,13 @@ public ApiResponse saveUser(@Valid @RequestBody JoinUserDto join return ApiResponse.success(responseJwtDto, ResponseCode.USER_CREATE_SUCCESS.getMessage()); } + // 토큰 검증 (Jwt 토큰을 서버에 전송하여, 서버가 유효한 토큰인지 확인하고 True 혹은 예외 반환) + @Operation(summary = "[토큰 검증] 토큰 검증", description = "클라이언트가 가지고 있던 Jwt 토큰을 서버에 전송하여, 서버가 유효한 토큰인지 확인하고 OK 혹은 예외를 반환합니다.") + @GetMapping("/token") + public ApiResponse tokenCheck(@RequestHeader String accessToken) { + return ApiResponse.success(jwtTokenProvider.validateAccessToken(accessToken), ResponseCode.TOKEN_CHECK_SUCCESS.getMessage()); + } + @Operation(summary = "[토큰 재발급] 토큰 재발급", description = "클라이언트가 가지고 있던 Refresh 토큰을 서버에 전송하여, 서버가 유효한 토큰인지 확인하고 OK 혹은 예외를 반환합니다.") @PostMapping("/reissue") public ApiResponse reissueToken(@RequestHeader String refreshToken) { diff --git a/src/main/java/com/diareat/diareat/util/exception/GlobalExceptionHandler.java b/src/main/java/com/diareat/diareat/util/exception/GlobalExceptionHandler.java index 856271e..56f64f1 100644 --- a/src/main/java/com/diareat/diareat/util/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/diareat/diareat/util/exception/GlobalExceptionHandler.java @@ -46,4 +46,11 @@ public ApiResponse> handleInValidRequestException(MethodArgu }); return ApiResponse.fail(ResponseCode.BAD_REQUEST, errors); } + + @ExceptionHandler(ValidException.class) // jwt 토큰 만료 관련 예외처리 + @ResponseStatus(HttpStatus.UNAUTHORIZED) + public ApiResponse handleValidException(ValidException e) { + log.info("Invalid Jwt Token: {}", e.getMessage()); + return ApiResponse.fail(e.getResponseCode(), null); + } } diff --git a/src/main/java/com/diareat/diareat/util/exception/ValidException.java b/src/main/java/com/diareat/diareat/util/exception/ValidException.java new file mode 100644 index 0000000..df30754 --- /dev/null +++ b/src/main/java/com/diareat/diareat/util/exception/ValidException.java @@ -0,0 +1,9 @@ +package com.diareat.diareat.util.exception; + +import com.diareat.diareat.util.api.ResponseCode; + +public class ValidException extends BaseException { + public ValidException(ResponseCode responseCode) { + super(responseCode); + } +}