Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,33 @@ public void logout(HttpServletRequest request, HttpServletResponse response,
throw new RestApiException(CommonErrorCode.NOT_EXIST_BEARER_SUFFIX);
}
String accessToken = bearerToken.substring(BEARER_PREFIX.length());
JwtUtil.validateAccessToken(accessToken, secretKey);

// 블랙 리스트에 어세스 토큰 추가
addToBlacklistRedis(accessToken);
try {
JwtUtil.validateAccessToken(accessToken, secretKey);

// 레디스에서 리프레시 토큰 삭제
String refreshToken = JwtUtil.getRefreshTokenCookies(request);
if (refreshToken != null) {
// Redis에서 해당 리프레시 토큰 키 삭제
refreshTokenRepository.findById(refreshToken)
.ifPresent(refreshTokenRepository::delete);
// 블랙 리스트에 어세스 토큰 추가
addToBlacklistRedis(accessToken);
} catch (RestApiException e) {
// 어세스토큰 만료는 정상 처리
if( !e.getErrorCode().equals(CommonErrorCode.EXPIRED_TOKEN))
throw e;
}

String refreshToken = JwtUtil.getRefreshTokenCookies(request);
// 쿠키에서 리프레시 토큰 삭제 (timeout을 0으로 두어 즉시 삭제)
JwtUtil.updateRefreshTokenCookie(response, null, 0);

// Redis에서 해당 리프레시 토큰 키 삭제
refreshTokenRepository.delete(
refreshTokenRepository.findById(refreshToken).orElseThrow( () ->
new RestApiException(CommonErrorCode.REFRESH_NOT_FOUND)
)
);

} catch (RestApiException e) {
// 쿠키나 레디스에서 리프레시 토큰을 찾지 못했을 경우 정상처리
if(e.getErrorCode().equals(CommonErrorCode.REFRESH_NOT_FOUND))
return;
try {
SendErrorResponseUtil.sendErrorResponse(response, e.getErrorCode());
} catch (IOException ex) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ureca/ufit/global/auth/util/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ public static String getRefreshTokenCookies(HttpServletRequest request) {
Cookie[] cookies = request.getCookies();

if (cookies == null)
throw new RestApiException(CommonErrorCode.REFRESH_DENIED);
throw new RestApiException(CommonErrorCode.REFRESH_NOT_FOUND);

for (Cookie cookie : cookies) {
if (REFRESH_TOKEN_COOKIE_NAME.equals(cookie.getName())) {
return cookie.getValue();
}
}

throw new RestApiException(CommonErrorCode.REFRESH_DENIED);
throw new RestApiException(CommonErrorCode.REFRESH_NOT_FOUND);
}

}