-
Notifications
You must be signed in to change notification settings - Fork 6k
OAuth 2.0 Client supports application clustering #7889
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
Comments
An alternative to storing |
Below is the screenshot of my project dependencies: And this is how my auth configure looks like in WebSecurity. I am using Spring JDBC session and annotated my main class with @EnableJdbcHttpSession as well.
Based on your above recommendation I have also added:
But still some of login request gets failed intermittently in my clustered environment. With error:
In DEBUG logs I can see this:
For authorization I am using: @jgrandja Can you please help in understanding what can be going wrong here? |
@Gomzee18 Looking at your config and logs, it's not clear why you are having this issue. It may be a misconfiguration on the Spring Session side since the log reveals:
|
Can I use a static HashMap to store the authorized requests instead? I believe that should work for statelessness on a single node. But definitely does not work for clustered environments. |
OAuth 2.0 Client should support a deployment topology where application instances are replicated and distributed across a cluster. This should be supported when using
HttpSecurity.oauth2Login()
and/orHttpSecurity.oauth2Client()
, as well for the reactive counterpart,ServerHttpSecurity.oauth2Login()
and/orServerHttpSecurity.oauth2Client()
.This can be supported today by using Spring Session and providing a minor customization for OAuth 2.0 Client. Spring Session allows replacing the
HttpSession
in the application container in a neutral way. For example, instead of using the application container'sHttpSession
implementation, Spring Session can store session data to either Redis, JDBC or Hazelcast. This is key to enable application clustering since application instances do not depend on the underlying container'sHttpSession
and instead retrieve session data from a remote/central backing store.In order to enable application clustering for OAuth 2.0 Client, you need to configure Spring Session and ensure the
HttpSession
implementations ofAuthorizationRequestRepository<OAuth2AuthorizationRequest>
andOAuth2AuthorizedClientRepository
are configured.The default
AuthorizationRequestRepository<OAuth2AuthorizationRequest>
isHttpSessionOAuth2AuthorizationRequestRepository
, so there is no need to configure this further.The default
OAuth2AuthorizedClientRepository
isAuthenticatedPrincipalOAuth2AuthorizedClientRepository
, which is backed by anInMemoryOAuth2AuthorizedClientService
that storesOAuth2AuthorizedClient
in-memory. This default configuration will not work in a clustered environment unless you have Session affinity (sticky sessions) configured. To override the default configuration and ensureOAuth2AuthorizedClient
are stored in the backingHttpSession
, provide the following configuration:The reactive counterpart:
The default serialization used by Spring Session is standard Java Serialization. However, if your requirements are to store session data in JSON format, this will be possible in the upcoming 5.3 release via #4886. To enable session data to be stored in JSON format when using Redis, add the following configuration (in addition to above):
See the Spring Session Redis-JSON sample for further details on how to configure.
The text was updated successfully, but these errors were encountered: