Sub topic 1
Where a Resource Server Fits in OAuth2
Prerequisites
This chapter assumes basic Spring Boot and Spring MVC, HTTP headers and status codes, and the difference between authentication and authorization. You should also know basic Java and Maven or Gradle.
Where a Resource Server Fits in OAuth2
OAuth2 defines roles rather than one specific server:
- The resource owner controls data, often a user.
- The client requests access to that data.
- The authorization server authenticates the owner and issues tokens.
- The resource server hosts the protected API and validates tokens.
For example, an order API can be a resource server. It does not usually log users in. It receives an access token from a client and decides whether that token permits access.
caption: OAuth2 request flow
alt: A client obtains an access token from an authorization server and sends it to a resource server.
flowchart TD
U[Resource owner] --> C[Client]
C --> AS[Authorization server]
AS --> C
C --> RS[Resource server API]
RS --> D[Protected data]Bearer Tokens on the Wire
A bearer token is a credential. Whoever possesses it can use it, so clients must protect it in transit and storage. The usual HTTP form is an Authorization header:
curl https://api.example.com/orders \
-H "Authorization: Bearer $ACCESS_TOKEN"The resource server checks the token before invoking the controller. A missing or invalid token normally produces 401 Unauthorized. A valid token without enough permission normally produces 403 Forbidden.
> callout warning > Always use HTTPS. A bearer token copied from a request log can be used by anyone until it expires or is revoked.
Anatomy of a JWT
A JSON Web Token (JWT) is a compact string with three Base64URL-encoded parts separated by periods:
- Header: metadata such as the signing algorithm and key identifier.
- Payload: claims, which are statements about the token.
- Signature: proof that the issuer signed the first two parts.
A JWT is encoded, not encrypted. Anyone holding it can usually read its payload. The signature protects integrity and authenticity. It does not hide sensitive values.
Reading the Claims That Matter
Common claims include:
iss: the token issuer.sub: the subject, such as a user or service.aud: the intended API.exp: expiration time.nbf: time before which the token is invalid.scopeorroles: application permissions.
A resource server should validate the issuer, audience, and time claims before using authorization claims.
How Signatures Are Verified: Algorithms and JWKS
A JSON Web Key Set (JWKS) is a published collection of public keys. Spring Security uses the token's key identifier, or kid, to select the matching public key and verify the signature. The authorization server keeps the private key.
The algorithm must be explicitly trusted. Never accept an unexpected algorithm merely because the token declares it. Key rotation changes the published keys, so the resource server must refresh its JWKS data when appropriate.
Exercise
Does a valid JWT signature prove that the caller may access every endpoint?
Show answer
No. It proves the token was issued by a trusted signer and was not altered. The resource server must still validate claims and apply authorization rules.