Skip to content
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

[스프린트 4] 장진영 - Refresh Token을 활용한 사용자 경험 개선 기능 추가 #38

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ResponseEntity<ApiResponse<?>> reissue(HttpServletRequest request, HttpSe

response.setHeader(AuthConstant.AUTHORIZATION, AuthConstant.BEARER + newAccess);
response.addCookie(CookieUtils.createCookie(AuthConstant.ACCESS_TOKEN, newAccess));
response.addCookie(CookieUtils.createCookie(AuthConstant.REFRESH_TOKEN, newRefresh));
response.addCookie(CookieUtils.createCookieWithHttpOnly(AuthConstant.REFRESH_TOKEN, newRefresh));

return ResponseEntity.status(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo

Optional<RefreshToken> findRefreshToken = refreshTokenService.findRefreshToken(customUserDetails.getMember().getId());

String refreshToken = null;
String refreshToken = jwtUtil.generateRefreshToken(uuid, role);

if (findRefreshToken.isEmpty()) {
refreshToken = jwtUtil.generateRefreshToken(uuid, role);
refreshTokenService.addRefreshEntity(refreshToken, uuid, jwtUtil.getRefreshExpiredTime());
} else {
refreshToken = findRefreshToken.get().getToken();
refreshTokenService.renewalRefreshToken(findRefreshToken.get().getToken(), refreshToken, jwtUtil.getRefreshExpiredTime());
}

String accessToken = jwtUtil.generateAccessToken(uuid, role);
Expand All @@ -71,9 +70,9 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
}
}

private void setInformationInResponse(HttpServletResponse response, String accessToken, String refreshToken) throws IOException {
private void setInformationInResponse(HttpServletResponse response, String accessToken, String refreshToken) {
Cookie access = CookieUtils.createCookie(ACCESS_TOKEN, accessToken);
Cookie refresh = CookieUtils.createCookie(REFRESH_TOKEN, refreshToken);
Cookie refresh = CookieUtils.createCookieWithHttpOnly(REFRESH_TOKEN, refreshToken);

response.addCookie(access);
response.addCookie(refresh);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@
@Slf4j
public class CookieUtils {

public static Cookie createCookie(String key, String value) {
public static Cookie createCookieWithHttpOnly(String key, String value) {
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(60 * 60);
//cookie.setSecure(true);
cookie.setSecure(true);
cookie.setPath("/");
cookie.setHttpOnly(true);

return cookie;
}

public static Cookie createCookie(String key, String value) {
Cookie cookie = new Cookie(key, value);
cookie.setMaxAge(60 * 60);
cookie.setSecure(true);
cookie.setPath("/");
cookie.setHttpOnly(false);

return cookie;
}

public static void clearCookie(HttpServletResponse response) {
Cookie cookie = new Cookie(REFRESH_TOKEN, null);
cookie.setMaxAge(0);
Expand Down