@EnableOAuth2Client belonged to the legacy Spring Security OAuth project (spring-security-oauth2). It switched on an OAuth2 client filter and an OAuth2ClientContext so an application could obtain access tokens from an authorization server. That project reached end of life, and the annotation does not exist in the current Spring Security OAuth2 client support. In a modern Spring Boot application you use spring-boot-starter-oauth2-client and oauth2Login() instead.
Many older tutorials describe @EnableOAuth2Client as the way to enable OAuth2 login. That is not correct for current Spring.
@EnableOAuth2Client was@EnableOAuth2Client wasThe annotation came from the old spring-security-oauth2 library (Spring Security OAuth 2.x). It was typically combined with OAuth2RestTemplate and OAuth2ProtectedResourceDetails to call a protected API. Spring announced the end of life of that project and moved the functionality into Spring Security 5 itself, which provides first-class OAuth 2.0 and OpenID Connect 1.0 client support. Spring Boot 2.x deprecated the old libraries and Spring Boot 3 does not support them, so new code should not use the annotation.
Current Spring Security gives you two related features:
oauth2Login()): users sign in through a provider such as Google, GitHub or your own OpenID Connect provider.oauth2Client() and OAuth2AuthorizedClientManager): your application obtains and stores access tokens to call other APIs on behalf of a user or itself.No @Enable... annotation is needed. Spring Boot auto-configures the client registrations from properties, and you declare a SecurityFilterChain bean.
Add the dependencies:
Register a provider in application.yml. Spring Security knows the endpoints for common providers (Google, GitHub, Okta, Facebook), so you only supply the credentials:
Read secrets from the environment or a secret store, never commit them. The default redirect URI is {baseUrl}/login/oauth2/code/{registrationId}, for example http://localhost:8080/login/oauth2/code/google, and you must register exactly that URI with the provider.
Declare the security configuration:
Visiting a protected page now redirects to the provider, completes the authorization code flow and returns the user to your application.
For OpenID Connect providers the principal is an OidcUser, which also exposes the ID token claims.
To call another service with the token Spring Security obtained, inject the authorized client for a registration:
Pass client.getAccessToken().getTokenValue() as a bearer token to the downstream API, or configure RestClient or WebClient with the OAuth2 support in Spring Security so tokens are attached and refreshed for you.
| Role | Starter | Purpose |
|---|---|---|
| OAuth2 client | spring-boot-starter-oauth2-client | Your app logs users in or fetches tokens |
| Resource server | spring-boot-starter-oauth2-resource-server | Your API validates incoming JWT or opaque bearer tokens |
| Authorization server | spring-boot-starter-oauth2-authorization-server | Your app issues tokens |
An application that only exposes an API protected by tokens needs the resource server, not the client.
@EnableOAuth2Client deprecated?It belongs to the end-of-life Spring Security OAuth project and is not part of current Spring Security. Migrate to oauth2Login() and oauth2Client().
OAuth2RestTemplate?Replace it with RestClient or WebClient configured with Spring Security's OAuth2 support, using an OAuth2AuthorizedClientManager to obtain tokens.
No. Add the starter, define client registrations in properties and call oauth2Login() in the SecurityFilterChain.
The redirect URI registered with the provider must match {baseUrl}/login/oauth2/code/{registrationId} exactly, including scheme and port.
@EnableOAuth2Client is a legacy annotation from a retired library. Current Spring Boot applications use spring-boot-starter-oauth2-client, properties for client registrations and a SecurityFilterChain with oauth2Login(). Use OAuth2AuthorizedClient support when you need access tokens for downstream calls.