Skip to content

Commit

Permalink
refactor: orgin cookie 프론트에 담기
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoya324 committed Nov 29, 2024
1 parent 707e49c commit 6c35d78
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.findy.findy_be.auth.api;

import static org.findy.findy_be.auth.oauth.handler.OAuth2AuthenticationSuccessHandler.*;

import java.util.Date;

import org.findy.findy_be.auth.api.swagger.AuthAPIPresentation;
Expand Down Expand Up @@ -89,7 +91,7 @@ public void refreshToken(HttpServletRequest request, HttpServletResponse respons

int cookieMaxAge = (int)refreshTokenExpiry / 60;
CookieUtil.deleteCookie(request, response, REFRESH_TOKEN);
CookieUtil.addCookie(response, REFRESH_TOKEN, authRefreshToken.getToken(), cookieMaxAge);
CookieUtil.addCookie(response, REFRESH_TOKEN, authRefreshToken.getToken(), cookieMaxAge, FRONT_DOMAIN);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS

private static final String BEARER = "Bearer ";
private static final String LOCAL_URL = "http://localhost:5173";
public static final String FRONT_DOMAIN = "findynow.com";
public static final String FRONT_LOCAL_DOMAIN = "localhost";

private final AuthTokenProvider tokenProvider;
private final AppProperties appProperties;
Expand All @@ -68,12 +70,13 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
int cookieMaxAge =
(int)new Date(System.currentTimeMillis() + appProperties.getAuth().getTokenExpiry()).getTime() / 60;

CookieUtil.addCookie(response, ACCESS_TOKEN, accessToken.getToken(), cookieMaxAge);
response.setHeader(accessHeader, BEARER + accessToken.getToken());

if (request.getRequestURI().contains("http://localhost:5173")) {
CookieUtil.addCookie(response, ACCESS_TOKEN, accessToken.getToken(), cookieMaxAge, FRONT_LOCAL_DOMAIN);
getRedirectStrategy().sendRedirect(request, response, "http://localhost:5173/map");
} else {
CookieUtil.addCookie(response, ACCESS_TOKEN, accessToken.getToken(), cookieMaxAge, FRONT_DOMAIN);
getRedirectStrategy().sendRedirect(request, response, "https://findynow.com/map");
}
clearAuthenticationAttributes(request, response);
Expand Down Expand Up @@ -128,7 +131,7 @@ protected String determineTargetUrl(HttpServletRequest request, HttpServletRespo
int cookieMaxAge = (int)refreshTokenExpiry / 60;

CookieUtil.deleteCookie(request, response, REFRESH_TOKEN);
CookieUtil.addCookie(response, REFRESH_TOKEN, refreshToken.getToken(), cookieMaxAge);
CookieUtil.addCookie(response, REFRESH_TOKEN, refreshToken.getToken(), cookieMaxAge, FRONT_DOMAIN);

return UriComponentsBuilder.fromUriString(LOCAL_URL)
.queryParam("token", accessToken.getToken())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.findy.findy_be.auth.oauth.repository;

import static org.findy.findy_be.auth.oauth.handler.OAuth2AuthenticationSuccessHandler.*;

import org.findy.findy_be.common.utils.CookieUtil;
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
Expand Down Expand Up @@ -36,10 +38,11 @@ public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationReq
}

CookieUtil.addCookie(response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME,
CookieUtil.serialize(authorizationRequest), cookieExpireSeconds);
CookieUtil.serialize(authorizationRequest), cookieExpireSeconds, FRONT_DOMAIN);
String redirectUriAfterLogin = request.getParameter(REDIRECT_URI_PARAM_COOKIE_NAME);
if (StringUtils.isNotBlank(redirectUriAfterLogin)) {
CookieUtil.addCookie(response, REDIRECT_URI_PARAM_COOKIE_NAME, redirectUriAfterLogin, cookieExpireSeconds);
CookieUtil.addCookie(response, REDIRECT_URI_PARAM_COOKIE_NAME, redirectUriAfterLogin, cookieExpireSeconds,
FRONT_DOMAIN);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public static Optional<Cookie> getCookie(HttpServletRequest request, String name
return Optional.empty();
}

public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) {
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge, String domain) {
Cookie cookie = new Cookie(name, value);
cookie.setPath("/");
cookie.setDomain(domain);
cookie.setHttpOnly(true);
cookie.setMaxAge(maxAge);

Expand Down

0 comments on commit 6c35d78

Please sign in to comment.