Skip to content

Commit

Permalink
feat: 쿠키 만료 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghwankim-dev committed Oct 16, 2024
1 parent df18aeb commit fbb89e0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Optional;
import java.util.regex.Pattern;

import org.springframework.http.ResponseCookie;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -54,6 +55,7 @@
import co.fineants.api.global.errors.exception.BadRequestException;
import co.fineants.api.global.errors.exception.FineAntsException;
import co.fineants.api.global.errors.exception.NotFoundResourceException;
import co.fineants.api.global.security.factory.TokenFactory;
import co.fineants.api.global.security.oauth.dto.Token;
import co.fineants.api.global.security.oauth.service.TokenService;
import co.fineants.api.global.util.CookieUtils;
Expand Down Expand Up @@ -93,6 +95,7 @@ public class MemberService {
private final TokenService tokenService;
private final OauthMemberRedisService oauthMemberRedisService;
private final RoleRepository roleRepository;
private final TokenFactory tokenFactory;

public void logout(HttpServletRequest request, HttpServletResponse response) {
// clear Authentication
Expand All @@ -112,6 +115,15 @@ public void logout(HttpServletRequest request, HttpServletResponse response) {
if (refreshToken != null) {
oauthMemberRedisService.banRefreshToken(refreshToken);
}

expiredCookies(response);
}

private void expiredCookies(HttpServletResponse response) {
ResponseCookie expiredAccessTokenCookie = tokenFactory.createExpiredAccessTokenCookie(Token.empty());
CookieUtils.setCookie(response, expiredAccessTokenCookie);
ResponseCookie expiredRefreshTokenCookie = tokenFactory.createExpiredRefreshTokenCookie(Token.empty());
CookieUtils.setCookie(response, expiredRefreshTokenCookie);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package co.fineants.api.global.security.factory;

import java.time.Duration;

import org.springframework.http.ResponseCookie;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -33,4 +35,26 @@ public ResponseCookie createRefreshTokenCookie(Token token) {
.httpOnly(true)
.build();
}

public ResponseCookie createExpiredAccessTokenCookie(Token token) {
return token.createAccessTokenCookie()
.domain(provider.domain())
.sameSite("None")
.path("/")
.secure(true)
.httpOnly(true)
.maxAge(Duration.ZERO)
.build();
}

public ResponseCookie createExpiredRefreshTokenCookie(Token token) {
return token.createRefreshTokenCookie()
.domain(provider.domain())
.sameSite("None")
.path("/")
.secure(true)
.httpOnly(true)
.maxAge(Duration.ZERO)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class Token {
private final String accessToken;
private final String refreshToken;

public static Token empty() {
return new Token(null, null);
}

public static Token create(String accessToken, String refreshToken) {
return new Token(accessToken, refreshToken);
}
Expand Down

0 comments on commit fbb89e0

Please sign in to comment.