Sub topic 1
Where a resource server fits in OAuth2
Prerequisites
This chapter assumes you have the following:
- Basic Spring Boot and Spring MVC knowledge: controllers, beans, and configuration properties.
- Familiarity with HTTP requests, headers, and status codes.
- A working idea of authentication (who are you?) versus authorization (what may you do?).
- Java 17 or later, with Maven or Gradle.
Why OAuth2 splits the work
OAuth2 is a framework for letting one piece of software act on a user's behalf without handing over the user's password. Instead of sharing a password, the user signs in once at a trusted service. That service issues a token, which is a string that proves the holder has been granted some access.
OAuth2 names four roles. The rest of this tutorial depends on telling them apart.
| Role | What it does | Example |
|---|---|---|
| Resource owner | Owns the data and grants access to it | A user of an order-tracking app |
| Client | Calls an API on the owner's behalf | A single-page app or a mobile app |
| Authorization server | Authenticates the owner and issues tokens | Keycloak, Okta, Spring Authorization Server |
| Resource server | Hosts the protected API and checks tokens | Your Spring Boot orders API |
The flow in one picture
Two points follow from the diagram:
- The resource server never sees the user's password. It only sees the token.
- The resource server does not issue tokens. It receives a token and decides whether to trust it.
What a resource server is responsible for
A resource server does three things on every request:
- Extracts the token from the request.
- Verifies that the token is genuine, unexpired, and meant for this API.
- Turns what the token says into permissions and applies your access rules.
It does not show login pages, run redirects, or store sessions. That is why the filter chain you build later is stateless. Each request carries its own proof.
A resource server is also different from a client. A Spring Boot app can be both, for example a gateway that calls other APIs. Spring Security configures the two roles separately, so keep them apart in your head. In this tutorial the application is only a resource server.
The Spring Boot starter
Spring Security provides the resource server role through one dependency. The Gradle form looks like this:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
}Once this starter is on the classpath, Spring Boot can auto-configure token handling. You still decide how tokens are validated and which endpoints need which permissions. The next chapters cover those decisions.
Common mistakes at this stage
- Treating the API as the login system. If your API has a
/loginendpoint that issues tokens, you are building an authorization server. Use an existing one unless you have a reason not to. - Confusing authentication with the token check. The user authenticated at the authorization server. Your API only verifies the result.
- Assuming the client is trusted. Anyone can send a request with any header. The token check is the only thing that lets you trust a call.
Exercise
A mobile app calls your Spring Boot orders API with a token that was issued by Keycloak. Name the OAuth2 role played by each of the three: the mobile app, Keycloak, and the orders API.
Show answer
The mobile app is the client. Keycloak is the authorization server. The orders API is the resource server. The user who signed in and approved access is the resource owner.