Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 로그아웃시 쿠키 삭제 및 RefreshToken 삭제 기능 구현 #207

Merged
merged 1 commit into from
Nov 14, 2024
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 @@ -35,4 +35,10 @@ public ReIssued getReIssuedRefreshTokenCookie(String username, String refreshTok

return TokenCommand.ReIssued.of(reIssuedAccessToken, reIssuedRefreshToken);
}

@Transactional
public void deleteRefreshToken(String refreshToken) {
String username = jwtUtil.getUsername(refreshToken);
refreshTokenService.deleteRefreshToken(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ public void saveRefreshToken(String username, String token) {
refreshTokenRepository.save(refreshToken);
}

public void deleteRefreshToken(String username) {
refreshTokenRepository.deleteById(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import team7.inplace.security.filter.TokenType;
Expand Down Expand Up @@ -47,4 +48,23 @@ private void addTokenToCookie(HttpServletResponse response, ReIssued reIssuedTok
response.addHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString());
}

@DeleteMapping("/refresh-token")
public ResponseEntity<Void> deleteRefreshToken(@CookieValue(value = "refresh_token") Cookie cookie,
HttpServletResponse response
) {
String refreshToken = cookie.getValue();
refreshTokenFacade.deleteRefreshToken(refreshToken);

ResponseCookie accessTokenCookie = CookieUtil.createCookie(
TokenType.ACCESS_TOKEN.getValue(),
"");
ResponseCookie refreshTokenCookie = CookieUtil.createCookie(
TokenType.REFRESH_TOKEN.getValue(),
"");
response.addHeader(HttpHeaders.SET_COOKIE, accessTokenCookie.toString());
response.addHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString());

return new ResponseEntity<>(HttpStatus.OK);
}

}