Skip to content

Commit

Permalink
[feat] 로그아웃시 쿠키 삭제 및 RefreshToken 삭제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
sanghee0820 committed Nov 14, 2024
1 parent c85ee52 commit e1cb588
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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);
}

}

0 comments on commit e1cb588

Please sign in to comment.