From 5cfa197cdd80adc2cb12674a54cce73c726756cd Mon Sep 17 00:00:00 2001 From: "DESKTOP-BC2AP8U\\Playdata" Date: Thu, 5 Dec 2024 04:32:32 +0900 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95:=20=ED=94=84=EB=A1=A0=ED=8A=B8=EC=97=94?= =?UTF-8?q?=EB=93=9C=20=EB=B0=B0=ED=8F=AC=20=EC=97=B0=EB=8F=99=20(?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=88=98=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/TokenController.java | 16 ++++++++++++++-- .../learnsmate/admin/service/RedisService.java | 18 +++++++++++------- .../intbyte4/learnsmate/security/JwtUtil.java | 7 ++++++- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/LearnsMate/src/main/java/intbyte4/learnsmate/admin/controller/TokenController.java b/LearnsMate/src/main/java/intbyte4/learnsmate/admin/controller/TokenController.java index 24a3d7e2..2cc0ffae 100644 --- a/LearnsMate/src/main/java/intbyte4/learnsmate/admin/controller/TokenController.java +++ b/LearnsMate/src/main/java/intbyte4/learnsmate/admin/controller/TokenController.java @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.*; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; @@ -48,11 +49,22 @@ public ResponseEntity logout(HttpServletRequest request, HttpServletResponse } } if (refreshToken == null || refreshToken.isEmpty()) { - log.warn("RefreshToken is missing in the request."); + log.warn("RefreshToken is missing in the request. Cookies: {}", Arrays.toString(request.getCookies())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("RefreshToken이 필요합니다."); } - String userCode = jwtUtil.getUserCodeFromToken(refreshToken); + if (!refreshToken.contains(".")) { + log.warn("Invalid RefreshToken format: {}", refreshToken); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("잘못된 RefreshToken 형식입니다."); + } + + String userCode = null; + try { + userCode = jwtUtil.getUserCodeFromToken(refreshToken); + } catch (Exception e) { + log.error("Failed to parse userCode from RefreshToken: {}", refreshToken, e); + } + if (userCode != null) { redisService.deleteToken(userCode); } else { diff --git a/LearnsMate/src/main/java/intbyte4/learnsmate/admin/service/RedisService.java b/LearnsMate/src/main/java/intbyte4/learnsmate/admin/service/RedisService.java index d63f289b..d7a273ac 100644 --- a/LearnsMate/src/main/java/intbyte4/learnsmate/admin/service/RedisService.java +++ b/LearnsMate/src/main/java/intbyte4/learnsmate/admin/service/RedisService.java @@ -16,22 +16,26 @@ public RedisService(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } - public void deleteToken(String userCode) { + public boolean deleteToken(String userCode) { if (userCode == null || userCode.isEmpty()) { - log.warn("삭제 요청 시 userCode가 null이거나 비어 있습니다."); - return; + log.warn("The userCode is null or empty in the delete request."); + return false; } - String key = "refreshToken:" + userCode; // Redis 키 형식 설정 + String key = "refreshToken:" + userCode; try { Boolean isDeleted = redisTemplate.delete(key); if (Boolean.TRUE.equals(isDeleted)) { - log.info("Redis에서 refreshToken 삭제 성공: {}", key); + log.info("Successfully deleted the refreshToken from Redis: {}", key); + return true; } else { - log.warn("Redis에서 refreshToken 삭제 실패 또는 키가 존재하지 않음: {}", key); + log.warn("Failed to delete the refreshToken from Redis or the key does not exist: {}", key); + log.info("Current keys in Redis: {}", redisTemplate.keys("refreshToken:*")); + return false; } } catch (Exception e) { - log.error("Redis에서 refreshToken 삭제 중 예외 발생: {}", e.getMessage(), e); + log.error("An exception occurred while deleting the refreshToken from Redis: {}", e.getMessage(), e); + return false; } } } diff --git a/LearnsMate/src/main/java/intbyte4/learnsmate/security/JwtUtil.java b/LearnsMate/src/main/java/intbyte4/learnsmate/security/JwtUtil.java index 2df4c251..6cbae69a 100644 --- a/LearnsMate/src/main/java/intbyte4/learnsmate/security/JwtUtil.java +++ b/LearnsMate/src/main/java/intbyte4/learnsmate/security/JwtUtil.java @@ -176,7 +176,12 @@ private Boolean isTokenExpired(String token) { } public String getUserCodeFromToken(String token) { - return getClaimFromToken(token, Claims::getSubject); + try { + return getClaimFromToken(token, Claims::getSubject); + } catch (JwtException | IllegalArgumentException e) { + log.error("Invalid JWT token: {}", token, e); + return null; + } } public String extractUserCode(String expiredToken) {