The spring-security-oauth2-client dependency adds OAuth 2.0 and OpenID Connect client support to a Spring application. It lets your app sign users in through a provider such as Google, GitHub or Keycloak (oauth2Login) and call other APIs with an access token (the OAuth2 client features). In Spring Boot you normally pull it in through spring-boot-starter-oauth2-client.
It is the replacement for the old, retired spring-security-oauth2 project, which should not be used anymore.
spring-security-oauth2-client is part of Spring Security. It implements the client side of the Authorization Code flow and related grants.
It does not make your app an authorization server and it does not validate bearer tokens for your own API. For that, use spring-security-oauth2-resource-server and Spring Authorization Server respectively.
In Spring Boot, use the starter, which also brings Spring Security and the needed OAuth2 libraries.
Gradle:
Check the starter name against the release you use, as Spring Boot occasionally reorganises its starters between major versions.
Spring Boot reads client registrations from spring.security.oauth2.client.registration.*. For well-known providers (Google, GitHub, Okta, Facebook) it already knows the endpoints, so you only provide credentials.
For any OpenID Connect provider, such as Keycloak, supply the issuer and Spring discovers the endpoints:
The default redirect URI is {baseUrl}/login/oauth2/code/{registrationId}. Register exactly that URL with the provider. Keep the client secret in an environment variable or a secret store, never in source control.
Define a SecurityFilterChain bean and turn on oauth2Login. Current Spring Security uses the lambda DSL.
Unauthenticated users are redirected to the provider, and after login Spring creates an OAuth2AuthenticationToken whose principal is an OidcUser or OAuth2User.
The client stores tokens as an OAuth2AuthorizedClient. You can inject it, or let an HTTP client attach the token for you. A RestClient or WebClient can use an interceptor or filter backed by OAuth2AuthorizedClientManager.
For machine-to-machine calls with no user, configure a registration with authorization-grant-type: client_credentials and use the authorized client manager.
| Dependency | Role |
|---|---|
spring-security-oauth2-client | Your app logs users in and calls APIs |
spring-security-oauth2-resource-server | Your API validates incoming tokens such as JWTs |
| Spring Authorization Server | Your own authorization server |
spring-security-oauth2 (legacy) | Retired; migrate away from it |
Yes, for Spring-based login with Google, GitHub and similar providers. The starter wires it up through configuration properties.
By default it is {baseUrl}/login/oauth2/code/{registrationId}, for example https://app.example.com/login/oauth2/code/google. It must match the one registered with the provider.
No. The client authenticates users and obtains tokens. The resource server protects your own API by validating tokens sent to it.
By default in the HTTP session through an authorized client repository. For stateless or clustered apps, configure a persistent OAuth2AuthorizedClientService or a session store.
spring-security-oauth2-client is the Spring Security module for OAuth 2.0 and OpenID Connect login and for calling APIs with tokens. Add the Boot starter, configure registrations in properties, enable oauth2Login in a SecurityFilterChain, and use the authorized client to forward tokens. Keep secrets out of code and avoid the legacy spring-security-oauth2 project.