-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: 쿠키 유틸리티 구현 * feat: 토큰 재발급 로직 구현 * feat: CORS 설정에 헤더 추가 * feat: 기존 컨트롤러 쿠키에 토큰 추가 * fix: 임시 회원가입 API로 다시 호출하여 토큰 받을 수 있게 수정 * fix: 오타 수정
- Loading branch information
Showing
5 changed files
with
95 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.depromeet.global.util; | ||
|
||
import com.depromeet.infra.config.jwt.JwtProperties; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CookieUtil { | ||
|
||
private final SpringEnvironmentUtil springEnvironmentUtil; | ||
private final JwtProperties jwtProperties; | ||
|
||
public void addTokenCookies( | ||
HttpServletResponse response, String accessToken, String refreshToken) { | ||
HttpHeaders headers = generateTokenCookies(accessToken, refreshToken); | ||
headers.forEach((key, value) -> response.addHeader(key, value.get(0))); | ||
} | ||
|
||
public HttpHeaders generateTokenCookies(String accessToken, String refreshToken) { | ||
|
||
String sameSite = determineSameSitePolicy(); | ||
|
||
ResponseCookie accessTokenCookie = | ||
ResponseCookie.from("accessToken", accessToken) | ||
.path("/") | ||
.maxAge(jwtProperties.accessTokenExpirationTime()) | ||
.secure(true) | ||
.sameSite(sameSite) | ||
.httpOnly(false) | ||
.build(); | ||
|
||
ResponseCookie refreshTokenCookie = | ||
ResponseCookie.from("refreshToken", refreshToken) | ||
.path("/") | ||
.maxAge(jwtProperties.refreshTokenExpirationTime()) | ||
.secure(true) | ||
.sameSite(sameSite) | ||
.httpOnly(false) | ||
.build(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add(HttpHeaders.SET_COOKIE, accessTokenCookie.toString()); | ||
headers.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); | ||
|
||
return headers; | ||
} | ||
|
||
private String determineSameSitePolicy() { | ||
if (springEnvironmentUtil.isProdProfile()) { | ||
return "Strict"; | ||
} | ||
return "None"; | ||
} | ||
} |