To secure a REST API with OAuth2 in Spring Boot, run the API as an OAuth2 resource server: add spring-boot-starter-oauth2-resource-server, set the issuer URI of your authorization server, and let Spring Security validate the JWT access token on every request. The API never sees user passwords; it only checks signed tokens and their scopes.
The spring-security-oauth2 project and @EnableAuthorizationServer are long retired. Today you use a dedicated authorization server (Keycloak, Auth0, Okta, Microsoft Entra ID, or Spring Authorization Server) and a resource server in your API.
| Role | Responsibility | Spring Boot module |
|---|---|---|
| Authorization server | Authenticates users and issues access tokens | Keycloak, Auth0, Okta or Spring Authorization Server |
| Resource server | Your REST API, validates tokens | spring-boot-starter-oauth2-resource-server |
| Client | Web, mobile or backend app that obtains tokens | spring-boot-starter-oauth2-client or any OAuth2 library |
A common mistake is adding the OAuth2 client starter and Google login settings to an API. That configures the "log in with Google" flow for a web application and does not protect a REST API. A REST API needs the resource server.
Add the dependencies:
Point the API at the issuer. Spring fetches the OpenID discovery document and the signing keys (JWKS) from it:
If your provider has no discovery endpoint, use spring.security.oauth2.resourceserver.jwt.jwk-set-uri instead. The issuer URI approach also validates the iss claim for you.
Spring Security converts each entry of the token's scope (or scp) claim into an authority with the SCOPE_ prefix, which is why the rules say SCOPE_orders.read. A missing or invalid token yields 401 with a WWW-Authenticate: Bearer header; a valid token without the scope yields 403.
Many providers put roles in a custom claim, such as roles or Keycloak's realm_access.roles. Map it with a converter:
Reference it with oauth.jwt(jwt -> jwt.jwtAuthenticationConverter(converter)), after which hasRole("ADMIN") works. To enforce the audience, add an OAuth2TokenValidator<Jwt> to the JwtDecoder built with JwtDecoders.fromIssuerLocation(...), combined with JwtValidators.createDefaultWithIssuer(...) through a DelegatingOAuth2TokenValidator. Without an audience check, a token issued for a different API could be accepted.
Inject the authenticated principal in a controller:
Request a token from the authorization server (for example with the client credentials grant for a service), then call the API:
If your provider issues opaque tokens instead of JWTs, use oauth.opaqueToken(...) with an introspection endpoint. If you must host the authorization server yourself, use Spring Authorization Server, which supports the authorization code flow with PKCE, client credentials and OpenID Connect. Public clients such as single-page and mobile apps should use the authorization code flow with PKCE; the implicit and password grants are removed from OAuth 2.1.
The client starter lets your app obtain tokens or log users in through a provider. The resource server starter protects your API by validating the tokens it receives.
JWTs are validated locally without a network call but cannot be revoked before expiry, so keep them short-lived. Opaque tokens are checked through introspection, which allows instant revocation at the cost of a call per request.
Check the issuer URI (it must match the iss claim exactly), the clock skew, the token's audience and that the header is Authorization: Bearer <token>. Debug logging for org.springframework.security shows the reason.
@EnableAuthorizationServer still available?No. It belonged to the retired Spring Security OAuth project. Use Spring Authorization Server or an external provider.
Protect a Spring Boot REST API with OAuth2 by making it a stateless resource server that validates JWTs from your authorization server. Configure the issuer URI, authorize with SCOPE_ authorities or mapped roles, check the audience, and keep tokens short-lived. Do not use the old spring-security-oauth2 annotations.