From 9b5d97cbcabc79cab994be76a3cc0629d2290383 Mon Sep 17 00:00:00 2001 From: Anna-Jin Date: Tue, 19 Jul 2022 03:01:24 +0900 Subject: [PATCH] =?UTF-8?q?#33=20[Update]=20=EC=98=88=EC=99=B8=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jwt 토큰 예외처리 추가 --- .../exception/GlobalExceptionHandler.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java b/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java index 7fb8076..fd6229d 100644 --- a/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java @@ -1,5 +1,9 @@ package com.mpnp.baechelin.exception; +import io.jsonwebtoken.ExpiredJwtException; +import io.jsonwebtoken.MalformedJwtException; +import io.jsonwebtoken.UnsupportedJwtException; +import io.jsonwebtoken.security.SignatureException; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -14,4 +18,28 @@ protected ResponseEntity handleCustomException(CustomException e) log.error("handleCustomException throw CustomException : {}", e.getErrorCode()); return ErrorResponse.toResponseEntity(e.getErrorCode()); } + + @ExceptionHandler(value = SignatureException.class) + protected ResponseEntity handleSignatureException(SignatureException e) { + log.error("잘못된 JWT 서명입니다."); + return ErrorResponse.toResponseEntity(ErrorCode.WRONG_TYPE_SIGNATURE); + } + + @ExceptionHandler(value = MalformedJwtException.class) + protected ResponseEntity handleMalformedJwtException(MalformedJwtException e) { + log.error("유효하지 않은 구성의 JWT 토큰입니다."); + return ErrorResponse.toResponseEntity(ErrorCode.WRONG_TYPE_TOKEN); + } + + @ExceptionHandler(value = ExpiredJwtException.class) + protected ResponseEntity handleExpiredJwtException(ExpiredJwtException e) { + log.error("만료된 JWT 토큰입니다."); + return ErrorResponse.toResponseEntity(ErrorCode.EXPIRED_TOKEN); + } + + @ExceptionHandler(value = UnsupportedJwtException.class) + protected ResponseEntity handleUnsupportedJwtException(UnsupportedJwtException e) { + log.error("지원되지 않는 형식이나 구성의 JWT 토큰입니다."); + return ErrorResponse.toResponseEntity(ErrorCode.WRONG_TYPE_TOKEN); + } }