-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: spring session repository cookie 방식으로 변경
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
core/core-security/src/main/java/com/mm/coresecurity/oauth/CookieUtils.java
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,54 @@ | ||
package com.mm.coresecurity.oauth; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.util.SerializationUtils; | ||
|
||
import java.util.Base64; | ||
import java.util.Optional; | ||
|
||
public class CookieUtils { | ||
|
||
public static Optional<Cookie> getCookie(HttpServletRequest request, String name) { | ||
Cookie[] cookies = request.getCookies(); | ||
if (cookies != null && cookies.length > 0) { | ||
for (Cookie cookie : cookies) { | ||
if (cookie.getName().equals(name)) { | ||
return Optional.of(cookie); | ||
} | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
public static void addCookie(HttpServletResponse response, String name, String value, int maxAge) { | ||
Cookie cookie = new Cookie(name, value); | ||
cookie.setPath("/"); | ||
cookie.setHttpOnly(true); | ||
cookie.setMaxAge(maxAge); | ||
response.addCookie(cookie); | ||
} | ||
|
||
public static void deleteCookie(HttpServletRequest request, HttpServletResponse response, String name) { | ||
Cookie[] cookies = request.getCookies(); | ||
if (cookies != null && cookies.length > 0) { | ||
for (Cookie cookie : cookies) { | ||
if (cookie.getName().equals(name)) { | ||
cookie.setValue(""); | ||
cookie.setPath("/"); | ||
cookie.setMaxAge(0); | ||
response.addCookie(cookie); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public static String serialize(Object object) { | ||
return Base64.getUrlEncoder().encodeToString(SerializationUtils.serialize(object)); | ||
} | ||
|
||
public static <T> T deserialize(Cookie cookie, Class<T> cls) { | ||
return cls.cast(SerializationUtils.deserialize(Base64.getUrlDecoder().decode(cookie.getValue()))); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...c/main/java/com/mm/coresecurity/oauth/HttpCookieOAuth2AuthorizationRequestRepository.java
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,49 @@ | ||
package com.mm.coresecurity.oauth; | ||
|
||
import com.nimbusds.oauth2.sdk.util.StringUtils; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class HttpCookieOAuth2AuthorizationRequestRepository implements AuthorizationRequestRepository<OAuth2AuthorizationRequest> { | ||
|
||
public static final String OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME = "oauth2_auth_request"; | ||
public static final String REDIRECT_URI_PARAM_COOKIE_NAME = "redirect_uri"; | ||
private static final int cookieExpireSeconds = 180; | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest loadAuthorizationRequest(HttpServletRequest request) { | ||
OAuth2AuthorizationRequest oAuth2AuthorizationRequest = CookieUtils.getCookie(request, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME) | ||
.map(cookie -> CookieUtils.deserialize(cookie, OAuth2AuthorizationRequest.class)) | ||
.orElse(null); | ||
return oAuth2AuthorizationRequest; | ||
} | ||
|
||
@Override | ||
public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationRequest, HttpServletRequest request, HttpServletResponse response) { | ||
if (authorizationRequest == null) { | ||
removeAuthorizationRequest(request, response); | ||
return; | ||
} | ||
|
||
CookieUtils.addCookie(response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME, CookieUtils.serialize(authorizationRequest), cookieExpireSeconds); | ||
String redirectUriAfterLogin = request.getParameter(REDIRECT_URI_PARAM_COOKIE_NAME); | ||
if (StringUtils.isNotBlank(redirectUriAfterLogin)) { | ||
CookieUtils.addCookie(response, REDIRECT_URI_PARAM_COOKIE_NAME, redirectUriAfterLogin, cookieExpireSeconds); | ||
} | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest removeAuthorizationRequest(HttpServletRequest request, HttpServletResponse response) { | ||
return this.loadAuthorizationRequest(request); | ||
} | ||
|
||
public void removeAuthorizationRequestCookies(HttpServletRequest request, HttpServletResponse response) { | ||
CookieUtils.deleteCookie(request, response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); | ||
CookieUtils.deleteCookie(request, response, REDIRECT_URI_PARAM_COOKIE_NAME); | ||
} | ||
|
||
} |