Package com.alpaca.security.oauth2
Class CookieAuthReqRepo
java.lang.Object
com.alpaca.security.oauth2.CookieAuthReqRepo
- All Implemented Interfaces:
org.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>
@Component
public class CookieAuthReqRepo
extends Object
implements org.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>
Cookie-based implementation of
AuthorizationRequestRepository for storing and retrieving
OAuth2AuthorizationRequest objects during OAuth2 login flows in a stateless manner.
By default, Spring Security stores the OAuth2 authorization request in the HTTP session using
HttpSessionOAuth2AuthorizationRequestRepository. This implementation replaces that
behavior by serializing the request into a secure cookie using CookieManager, enabling
stateless authentication flows (e.g., REST APIs or mobile clients).
Two cookies are used:
oauth2_auth_request: holds the serializedOAuth2AuthorizationRequest.redirect_uri: optionally stores a post-login redirect URI provided by the client.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequestloadAuthorizationRequest(jakarta.servlet.http.HttpServletRequest request) Loads theOAuth2AuthorizationRequestfrom the cookie if present.org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequestremoveAuthorizationRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Removes the authorization request.voidremoveAuthorizationRequestCookies(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Deletes both the authorization request and redirect URI cookies.voidsaveAuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest authorizationRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Saves theOAuth2AuthorizationRequestinto a cookie, and optionally captures a redirect URI to be stored in a separate cookie.
-
Field Details
-
AuthorizationCookieName
- See Also:
-
RedirectCookieName
- See Also:
-
cookieExpiredSeconds
public static final int cookieExpiredSeconds- See Also:
-
-
Constructor Details
-
CookieAuthReqRepo
public CookieAuthReqRepo()
-
-
Method Details
-
loadAuthorizationRequest
public org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest loadAuthorizationRequest(jakarta.servlet.http.HttpServletRequest request) Loads theOAuth2AuthorizationRequestfrom the cookie if present.- Specified by:
loadAuthorizationRequestin interfaceorg.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>- Parameters:
request- the incoming HTTP request- Returns:
- the deserialized
OAuth2AuthorizationRequest, ornullif not found
-
saveAuthorizationRequest
public void saveAuthorizationRequest(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest authorizationRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Saves theOAuth2AuthorizationRequestinto a cookie, and optionally captures a redirect URI to be stored in a separate cookie.If
authorizationRequestisnull, deletes both cookies.- Specified by:
saveAuthorizationRequestin interfaceorg.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>- Parameters:
authorizationRequest- the OAuth2 request to save (ornullto clear)request- the incoming HTTP requestresponse- the outgoing HTTP response to which cookies will be added
-
removeAuthorizationRequest
public org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest removeAuthorizationRequest(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Removes the authorization request. This implementation defers toloadAuthorizationRequest(jakarta.servlet.http.HttpServletRequest).- Specified by:
removeAuthorizationRequestin interfaceorg.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>- Parameters:
request- the HTTP requestresponse- the HTTP response- Returns:
- the previously stored
OAuth2AuthorizationRequest, ornullif not available
-
removeAuthorizationRequestCookies
public void removeAuthorizationRequestCookies(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Deletes both the authorization request and redirect URI cookies.- Parameters:
request- the HTTP requestresponse- the HTTP response
-