Skip to content

Commit

Permalink
♻️ 코드 수정: 프론트엔드 배포 연동 (로그아웃 기능 수정)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoJeHuni committed Dec 4, 2024
1 parent 12da299 commit 5cfa197
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,26 @@ public RedisService(RedisTemplate<String, String> 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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5cfa197

Please sign in to comment.