From e1cb588f5462b9730563a17b58a3e3241f3a45c1 Mon Sep 17 00:00:00 2001 From: sanghee0820 Date: Fri, 15 Nov 2024 04:40:37 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EB=A1=9C=EA=B7=B8=EC=95=84=EC=9B=83?= =?UTF-8?q?=EC=8B=9C=20=EC=BF=A0=ED=82=A4=20=EC=82=AD=EC=A0=9C=20=EB=B0=8F?= =?UTF-8?q?=20RefreshToken=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../token/application/RefreshTokenFacade.java | 6 ++++++ .../application/RefreshTokenService.java | 3 +++ .../presentation/RefreshTokenController.java | 20 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java b/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java index 9afd9b39..b5c611c3 100644 --- a/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java +++ b/src/main/java/team7/inplace/token/application/RefreshTokenFacade.java @@ -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); + } } diff --git a/src/main/java/team7/inplace/token/application/RefreshTokenService.java b/src/main/java/team7/inplace/token/application/RefreshTokenService.java index f087a793..a77290c6 100644 --- a/src/main/java/team7/inplace/token/application/RefreshTokenService.java +++ b/src/main/java/team7/inplace/token/application/RefreshTokenService.java @@ -27,4 +27,7 @@ public void saveRefreshToken(String username, String token) { refreshTokenRepository.save(refreshToken); } + public void deleteRefreshToken(String username) { + refreshTokenRepository.deleteById(username); + } } diff --git a/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java b/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java index 764d93ca..add0c99d 100644 --- a/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java +++ b/src/main/java/team7/inplace/token/presentation/RefreshTokenController.java @@ -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; @@ -47,4 +48,23 @@ private void addTokenToCookie(HttpServletResponse response, ReIssued reIssuedTok response.addHeader(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); } + @DeleteMapping("/refresh-token") + public ResponseEntity 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); + } + }