Spring OAuth2 SSO工作原理、重定向逻辑及客户端运行机制咨询
Awesome that you've got a working Hello World example for Spring OAuth2 SSO up and running! Let's dive into your two questions with clear, straightforward explanations:
OAuth2 SSO is all about letting multiple apps share a single identity provider (the authorization server) so users don't have to log into each app separately. Spring simplifies this process with built-in auto-configuration, and here's exactly how it works:
- Initial Request Trigger: When a user tries to access a protected resource in your client app, Spring's SSO filter (auto-configured by
@EnableOAuth2Sso) detects the user isn't authenticated. It immediately redirects the user to the authorization server's login page. - Authorization Request: The client app sends along its
client-id, requested permissions (scopes), and a pre-configured redirect URI to the authorization server. This tells the auth server which app is asking for access, and where to send the user back after login. - User Authentication & Consent: The user logs into the authorization server (enters their credentials). If it's the first time this app is requesting access, the auth server will ask the user to grant permission to the client app.
- Authorization Code Redirect: Once the user is authenticated and grants consent, the auth server generates a short-lived authorization code, then redirects the user back to the client app's redirect URI—with the authorization code attached as a URL parameter.
- Token Exchange (Backend): The client app takes that authorization code, and in a server-to-server background request, sends it to the auth server along with its
client-idandclient-secretto exchange it for an access token (and usually a refresh token). - User Session Creation: With the access token, the client app fetches the user's profile info from the auth server, then creates a local user session. Now the user can access the protected resources in the client app.
Why the redirect flow?
This multi-step redirect process is all about security:
- The client app never touches the user's password—all authentication happens on the authorization server, reducing the risk of credential leaks.
- The authorization code is only passed once via a browser redirect, and the actual token exchange happens in a private backend call, making it harder for attackers to intercept sensitive tokens.
- It follows the official OAuth2 authorization code flow, which is the most secure flow for server-side applications.
UiSecurityConfig Works Your UiSecurityConfig class might be small, but it's the backbone of your client app's SSO setup. Let's break down each part:
@Configuration: Marks this class as a Spring configuration bean—Spring will scan and load all the security settings defined here.@EnableOAuth2Sso: This is the magic annotation! It auto-configures everything the client app needs to participate in SSO:- Registers an SSO filter that handles unauthenticated requests, redirects to the auth server, and processes the authorization code callback.
- Sets up an
OAuth2RestTemplatefor secure communication between the client and auth server (like exchanging the authorization code for tokens). - Manages the
OAuth2ClientContextto store tokens and session-related OAuth2 data.
- Extending
WebSecurityConfigurerAdapter: This is Spring Security's base class for customizing security rules. By extending it, you can:- Override
configure(HttpSecurity http)to define which paths are protected, which are public (like static assets), or customize logout behavior. For example, you might add.antMatchers("/css/**", "/js/**").permitAll()to let static resources load without authentication. - Override other methods if you need to tweak authentication managers, but in SSO mode, you rarely need this since authentication is handled by the auth server.
- Override
In short, @EnableOAuth2Sso does all the heavy lifting of wiring up the OAuth2 flow, while extending WebSecurityConfigurerAdapter lets you fine-tune the client app's security rules to fit your needs.
内容的提问来源于stack exchange,提问作者gstackoverflow

