Sub topic 1
Where a Resource Server Fits in OAuth2
Prerequisites
This chapter assumes you already know the following:
- Basic Spring Boot and Spring MVC: controllers, beans, and configuration properties.
- How HTTP requests, headers, and status codes work.
- The difference between authentication (who are you?) and authorization (what may you do?).
- Java 17 or later, with Maven or Gradle.
You do not need to know anything about JWTs yet. Later chapters cover them. This chapter only places your Spring Boot API inside the larger OAuth2 picture, so the configuration in later chapters makes sense.
The problem OAuth2 solves
Suppose a mobile app needs to read a user's orders from an API. The simplest design is for the user to type their username and password into the app, and for the app to send those credentials with every request. That design has serious problems:
- The app sees the user's password and can misuse it.
- The app gets full access, even if it only needs to read orders.
- The user cannot revoke the app's access without changing the password.
- Every API has to handle passwords itself.
OAuth2 is a framework for delegated authorization. Instead of sharing a password, the user signs in at a dedicated server. That server issues the app a limited, short-lived token. The app presents the token to the API. The API checks the token and decides whether to serve the request.
OAuth2 is about authorization, meaning what a client may do. It does not define how a user logs in. Login and identity are handled by OpenID Connect, which is built on top of OAuth2. A resource server does not deal with login at all.
The four roles
OAuth2 defines four roles. One piece of software can play more than one role, but keeping them separate in your head makes the rest of the tutorial easier.
| Role | What it is | Example |
|---|---|---|
| Resource owner | The party that owns the data and can grant access to it. Usually a user. | A customer who owns their orders |
| Client | The application that wants to access the data on the owner's behalf. | A mobile app, a single-page app, or another backend service |
| Authorization server | Authenticates the owner, obtains consent, and issues tokens. | Keycloak, Okta, Auth0, Spring Authorization Server |
| Resource server | The API that holds the data and accepts or rejects requests based on the token. | Your Spring Boot orders API |
The word "client" here does not mean "browser". A server-side application that calls another API is also an OAuth2 client.
This tutorial focuses on the last role. Your Spring Boot application is the resource server. It does not log users in, it does not issue tokens, and it does not talk to the user's browser about credentials. It receives a token with each request and answers one question: should I trust this token enough to serve this request?
How the roles interact
The diagram below shows a typical flow. The exact steps for getting a token depend on the grant type. The grant type is the OAuth2 procedure the client uses to get a token, such as the authorization code flow for user-facing apps or the client credentials flow for service-to-service calls. The resource server's part is the same in every case.
Notice what is absent from the last three steps. The resource server never sees the user's password, and in the common JWT setup it does not call the authorization server for each request. It checks the token itself, using information it obtained from the authorization server beforehand.
What the resource server is responsible for
A resource server has a short list of jobs:
- Extract the access token from the incoming request.
- Validate the token. It must confirm the token is genuine, unexpired, and meant for this API.
- Build an authenticated identity from the token, including the permissions it grants.
- Apply authorization rules to decide whether this caller may perform this operation.
- Return
401or403with a correct error response when the answer is no.
Each job maps to a later chapter. Token extraction is covered under bearer tokens. Validation covers JWT signatures, claims, and the JwtDecoder. Identity and permissions are built by the authority mapping. Rules come from request matchers and @PreAuthorize. Errors are covered in the 401 and 403 chapter.
What the resource server is not responsible for
Equally important is what to leave out of your API:
- Login pages and password checks. The authorization server owns these.
- Issuing or refreshing tokens. Only the authorization server does this.
- Consent screens. These also belong to the authorization server.
- Sessions. A resource server is normally stateless. Every request carries its own proof of identity, and the server stores nothing between requests.
If you find yourself writing a login endpoint in your resource server, you have probably mixed up the roles.
Resource server versus client in Spring Security
Spring Security ships separate support for the different roles, and mixing them up is a common source of confusion. The dependency you add determines which role you configure.
| Role | Spring Boot starter | Configuration prefix |
|---|---|---|
| Resource server | `spring-boot-starter-oauth2-resource-server` | `spring.security.oauth2.resourceserver.*` |
| Client (including "log in with ..." support) | `spring-boot-starter-oauth2-client` | `spring.security.oauth2.client.*` |
| Authorization server | `spring-boot-starter-oauth2-authorization-server` | `spring.security.oauth2.authorizationserver.*` |
A pure API only needs the resource server starter. A web application that has its own login page and also exposes an API may use both the client and the resource server support. In that case keep them in separate security filter chains, so that each chain has one clear purpose.
Here is the Maven dependency for the resource server role:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>The starter pulls in Spring Security and the library that parses and verifies JWTs. With only this dependency and one property, Spring Boot can already protect your API. A later chapter shows that configuration.
What the resource server needs to know in advance
Because the resource server trusts tokens it did not issue, it needs some configuration that ties it to a specific authorization server:
- The issuer. This is the identity of the authorization server, written as a URL such as
https://issuer.example.com/realms/demo. Tokens from any other issuer must be rejected. - A way to verify tokens. For JWTs, this is the set of public keys published by the authorization server. For opaque tokens, it is an introspection endpoint where the resource server asks the authorization server whether a token is valid.
- Its own identity as an audience. An authorization server may issue tokens for many APIs. The resource server should accept only tokens meant for it, for example one whose audience is
orders-api.
The first two items are mandatory for the application to work. The third is optional in Spring's defaults, but you should treat it as required for any real deployment. Without it, a token issued for a different API at the same authorization server might be accepted by yours.
Trust is the central idea
The key point of this chapter is the trust relationship. The resource server delegates two things to the authorization server: authenticating the user and deciding what the client may access. In return, it must verify that each token really came from that authorization server and was meant for this API.
This has practical consequences:
- If validation is too lax, anyone who can craft a token-shaped string can call your API.
- If validation is correct but your rules are too coarse, a valid token with a small scope might reach operations it should not.
- The permissions in the token describe what the authorization server granted. Your API decides what those permissions mean for each endpoint.
Common mistakes at this stage
- Treating the resource server as the login system. Redirects to a login page make sense for a browser application, not for an API. An API should answer
401and let the client obtain a token. - Adding the client starter when you only need the resource server. The client starter enables login-related behavior and extra configuration you do not want on a pure API.
- Skipping the audience check. The default setup validates the issuer and timestamps but not who the token was meant for. A later chapter shows how to add this.
- Assuming the resource server can revoke tokens. In the JWT model the resource server does not call the authorization server on each request, so it has no built-in way to learn that a token was cancelled early. The opaque token chapter compares the trade-offs.
Summary
- OAuth2 lets a client access a resource owner's data through a limited token, with no password sharing.
- The four roles are resource owner, client, authorization server, and resource server.
- Your Spring Boot API is the resource server. It validates tokens and enforces authorization rules. It does not authenticate users or issue tokens.
- Spring Security provides separate starters for each role. A pure API needs
spring-boot-starter-oauth2-resource-server. - The resource server needs to know the issuer, how to verify tokens, and which audience it represents.
Exercise
A team builds an orders API with Spring Boot. A developer adds a /login endpoint to the API that checks a username and password and returns a token. Which OAuth2 roles is the API now playing, and what should the team do to keep the roles separate?
Show answer
The API is playing both the resource server role (serving orders) and the authorization server role (authenticating users and issuing tokens). To keep the roles separate, move login and token issuance to a dedicated authorization server such as Keycloak, Okta, or Spring Authorization Server. The orders API should then act only as a resource server, validating the tokens that the authorization server issues and enforcing authorization rules.
Exercise
In the flow shown in the diagram, which party does the resource server need to trust, and what three pieces of configuration tie the resource server to that party?
Show answer
The resource server must trust the authorization server. It needs to know the issuer (the identity of the authorization server), a way to verify tokens (the public keys for JWTs, or an introspection endpoint for opaque tokens), and its own audience, so it accepts only tokens meant for this API.