Sub topic 14
Opaque Tokens and Introspection as an Alternative
This chapter assumes you have a working JWT resource server, as built in the earlier chapters. You should know what a bearer token is and how a SecurityFilterChain is configured.
What an opaque token is
A JWT carries its claims inside itself. The resource server verifies the signature locally and needs no network call. An opaque token is different. It is a random string with no readable structure. The resource server cannot read it, so it must ask the authorization server what the token means.
That question goes to the introspection endpoint, defined in RFC 7662. The resource server sends the token. The authorization server replies with JSON that includes active, and usually scope, sub, exp, and client_id.
Configuring introspection
Replace the jwt properties with opaquetoken properties. The resource server authenticates to the introspection endpoint with its own client credentials, so read the secret from the environment and never commit it.
spring:
security:
oauth2:
resourceserver:
opaquetoken:
introspection-uri: https://issuer.example.com/oauth2/introspect
client-id: orders-api
client-secret: ${INTROSPECTION_CLIENT_SECRET}
# In the filter chain:
# .oauth2ResourceServer(oauth2 -> oauth2.opaqueToken(Customizer.withDefaults()))In the filter chain, call opaqueToken(...) instead of jwt(...). You can't configure both on the same chain with default settings, because Spring Security needs to know which kind of token to expect.
Spring Boot also needs the oauth2-oidc-sdk library on the classpath for this mode. The spring-boot-starter-oauth2-resource-server starter normally includes it.
What changes in your code
Authority mapping still works. By default, Spring turns each value of the scope field in the introspection response into a SCOPE_ authority. Your existing rules such as hasAuthority("SCOPE_orders.read") and @PreAuthorize keep working unchanged.
The principal type changes. With JWTs you inject a Jwt. With opaque tokens the principal is an OAuth2AuthenticatedPrincipal:
@GetMapping("/me")
Map<String, Object> me(@AuthenticationPrincipal OAuth2AuthenticatedPrincipal principal) {
return Map.of("name", principal.getName(), "scope", principal.getAttribute("scope"));
}Code that expects a Jwt will fail with a type error or receive null, so check your controllers when you switch.
JWT or opaque: the trade-offs
| Concern | JWT (local validation) | Opaque (introspection) |
|---|---|---|
| Network call per request | No | Yes, unless cached |
| Revocation | Hard. A token stays valid until `exp`. | Immediate. The next introspection returns `active: false`. |
| Token contents visible to client | Yes, the payload is only encoded | No |
| Load on authorization server | Low | Grows with API traffic |
| Failure mode | Stale keys | Authorization server outage blocks all requests |
Choose opaque tokens when you need instant revocation or do not want claims exposed to clients. Choose JWTs when you need low latency and want the API to work without calling the authorization server on each request. Some systems cache introspection results for a short time to reduce load. That brings back a small revocation delay equal to the cache lifetime.
Exercise
Your API must stop accepting a token within seconds of a user logging out. You also see that every request now adds a call to the authorization server. Name one way to reduce that load and the cost it brings.
Show answer
Cache introspection results for a short time, for example a few seconds to a minute. This cuts calls to the authorization server. The cost is that a revoked token can still be accepted until its cached result expires, so the cache lifetime sets the revocation delay.