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.OAuth2AuthorizationRequest
loadAuthorizationRequest
(jakarta.servlet.http.HttpServletRequest request) Loads theOAuth2AuthorizationRequest
from the cookie if present.org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest
removeAuthorizationRequest
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Removes the authorization request.void
removeAuthorizationRequestCookies
(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Deletes both the authorization request and redirect URI cookies.void
saveAuthorizationRequest
(org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest authorizationRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) Saves theOAuth2AuthorizationRequest
into 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 theOAuth2AuthorizationRequest
from the cookie if present.- Specified by:
loadAuthorizationRequest
in interfaceorg.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>
- Parameters:
request
- the incoming HTTP request- Returns:
- the deserialized
OAuth2AuthorizationRequest
, ornull
if 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 theOAuth2AuthorizationRequest
into a cookie, and optionally captures a redirect URI to be stored in a separate cookie.If
authorizationRequest
isnull
, deletes both cookies.- Specified by:
saveAuthorizationRequest
in interfaceorg.springframework.security.oauth2.client.web.AuthorizationRequestRepository<org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest>
- Parameters:
authorizationRequest
- the OAuth2 request to save (ornull
to 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:
removeAuthorizationRequest
in 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
, ornull
if 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
-