-
Notifications
You must be signed in to change notification settings - Fork 1
[FEAT] 쿠키 설정 추가 #143
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
Open
wlqgkrry
wants to merge
1
commit into
dev
Choose a base branch
from
feature/reissue-v1
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[FEAT] 쿠키 설정 추가 #143
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/deardream/deardream_be/domain/cookie/CookieService.java
This file contains hidden or 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,55 @@ | ||
| package com.deardream.deardream_be.domain.cookie; | ||
|
|
||
| import com.deardream.deardream_be.domain.cookie.CookieUtil; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| @Service | ||
| public class CookieService { | ||
|
|
||
| public static final long ACCESS_TOKEN_EXPIRES_IN = 60 * 60 * 2; | ||
| public static final long REFRESH_TOKEN_EXPIRES_IN = 60 * 60 * 24 * 2; | ||
| public static final long TEMP_TOKEN_EXPIRES_IN = 60 * 10; | ||
|
|
||
| public void setAccessTokenCookie(HttpServletResponse response, String token) { | ||
| response.addHeader("Set-Cookie", CookieUtil.createAccessTokenCookie(token, ACCESS_TOKEN_EXPIRES_IN).toString()); | ||
| } | ||
|
|
||
|
|
||
| public void setRefreshTokenCookie(HttpServletResponse response, String token) { | ||
| response.addHeader("Set-Cookie", CookieUtil.createRefreshTokenCookie(token, REFRESH_TOKEN_EXPIRES_IN).toString()); | ||
| } | ||
|
|
||
| public void setTempTokenCookie(HttpServletResponse response, String token) { | ||
| response.addHeader("Set-Cookie", CookieUtil.createTempTokenCookie(token, TEMP_TOKEN_EXPIRES_IN).toString()); | ||
| } | ||
|
|
||
| // 토큰 삭제(만료) | ||
| public void deleteAccessTokenCookie(HttpServletResponse response) { | ||
| response.addHeader("Set-Cookie", CookieUtil.deleteAccessTokenCookie().toString()); | ||
| } | ||
|
|
||
| public void deleteRefreshTokenCookie(HttpServletResponse response) { | ||
| response.addHeader("Set-Cookie", CookieUtil.deleteRefreshTokenCookie().toString()); | ||
| } | ||
|
|
||
| public void deleteTempTokenCookie(HttpServletResponse response) { | ||
| response.addHeader("Set-Cookie", CookieUtil.deleteTempTokenCookie().toString()); | ||
| } | ||
|
|
||
| public void setTokenCookies(HttpServletResponse response, String accessToken, String refreshToken) { | ||
| if (accessToken != null) { | ||
| setAccessTokenCookie(response, accessToken); | ||
| } | ||
| if (refreshToken != null) { | ||
| setRefreshTokenCookie(response, refreshToken); | ||
| } | ||
| } | ||
|
|
||
| public void deleteTokenCookies(HttpServletResponse response) { | ||
| deleteAccessTokenCookie(response); | ||
| deleteRefreshTokenCookie(response); | ||
| } | ||
|
|
||
|
|
||
| } |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/deardream/deardream_be/domain/cookie/CookieUtil.java
This file contains hidden or 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,80 @@ | ||
| package com.deardream.deardream_be.domain.cookie; | ||
|
|
||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import org.springframework.http.ResponseCookie; | ||
| import org.springframework.http.ResponseEntity; | ||
|
|
||
| public class CookieUtil { | ||
|
|
||
| public static final String ACCESS_TOKEN = "accessToken"; | ||
| public static final String REFRESH_TOKEN = "refreshToken"; | ||
| public static final String TEMP_TOKEN = "tempToken"; | ||
|
|
||
| // 필요하다면 SameSite 값을 "Strict"나 "Lax", "None" 등으로 변경 | ||
| private static final String SAME_SITE = "Strict"; | ||
| private static final String PATH = "/"; | ||
|
|
||
| // 액세스 토큰 세팅 | ||
| public static ResponseCookie createAccessTokenCookie(String token, long maxAgeSeconds) { | ||
| return ResponseCookie.from(ACCESS_TOKEN, token) | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(maxAgeSeconds) | ||
| .sameSite(SAME_SITE) | ||
| .build(); | ||
| } | ||
|
|
||
| // 리프레시 토큰 세팅 | ||
| public static ResponseCookie createRefreshTokenCookie(String token, long maxAgeSeconds) { | ||
| return ResponseCookie.from(REFRESH_TOKEN, token) | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(maxAgeSeconds) | ||
| .sameSite(SAME_SITE) | ||
| .build(); | ||
| } | ||
|
|
||
| public static ResponseCookie createTempTokenCookie(String token, long maxAgeSeconds) { | ||
| return ResponseCookie.from(TEMP_TOKEN, token) | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(maxAgeSeconds) | ||
| .sameSite(SAME_SITE) | ||
| .build(); | ||
| } | ||
|
|
||
| // 액세스 토큰 쿠키 삭제 (로그아웃 등) | ||
| public static ResponseCookie deleteAccessTokenCookie() { | ||
| return ResponseCookie.from(ACCESS_TOKEN, "") | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(0) | ||
| .sameSite(SAME_SITE) | ||
| .build(); | ||
| } | ||
|
|
||
| // 리프레시 토큰 쿠키 삭제 | ||
| public static ResponseCookie deleteRefreshTokenCookie() { | ||
| return ResponseCookie.from(REFRESH_TOKEN, "") | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(0) | ||
| .sameSite(SAME_SITE) | ||
| .build(); | ||
| } | ||
|
|
||
| public static ResponseCookie deleteTempTokenCookie() { | ||
| return ResponseCookie.from(TEMP_TOKEN, "") | ||
| .httpOnly(true) | ||
| .secure(true) | ||
| .path(PATH) | ||
| .maxAge(0) | ||
| .sameSite(SAME_SITE) | ||
| .build(); | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 재발급 = 리프레쉬 토큰 이라고 새각했는데 아니네요! 새 토큰 발급받는 api 였구뇨!