Resource server token validation
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
The resource server (port 8081) accepts a request only with an access token that the authorization server issued for
it. By default tokens are self-contained JWTs, validated locally against the authorization server’s published keys:
signature, issuer, timestamps and audience, then scopes and roles. Under the opaque profile the resource server
instead introspects reference tokens at the authorization server (RFC 7662), authenticating as a client of its own.
Clients can revoke tokens (RFC 7009); an introspecting resource server sees the revocation at once, a JWT-validating one
when the token expires. Failures follow RFC 6750.
-
Actors: a client holding an access token (the BFF,
service-client, …),resource-server,auth-server. -
Clients involved: the client the token was issued to, whose configured audiences must include
resource-server; underopaque, a confidential client for the resource server withclient_secret_basic(any confidential client can introspect), and token clients registered with"accessTokenFormat": "reference". -
Preconditions:
-
JWT (default, profile not
opaque):spring.security.oauth2.resourceserver.jwt.issuer-uri(AUTH_ISSUER, defaulthttp://localhost:9000) and…jwt.audiences(RESOURCE_SERVER_AUDIENCES, defaultresource-server; the application refuses to start when it is empty). -
Introspection: Spring profile
opaqueandAUTH_INTROSPECTION_URI,INTROSPECTION_CLIENT_ID,INTROSPECTION_CLIENT_SECRET(no defaults); the same audiences setting.
-
-
Endpoints:
GET /api/documents,DELETE /api/documents/{id}(resource server);/.well-known/openid-configuration,/oauth2/jwks,POST /oauth2/introspect,POST /oauth2/revoke(authorization server).
Steps
JWT validation (default)
-
A request arrives with
Authorization: Bearer <JWT>(orDPoP <JWT>with a proof, see Sender-constrained tokens). -
SecurityConfig.resourceServerFilterChain(@Profile("!opaque")) uses the decoder Spring Boot builds from the issuer URI. On the first request it reads the issuer’s metadata and itsjwks_uri(/oauth2/jwks), which publishes the active, next and still-valid retired keys, so tokens signed before a key rotation still verify (see Tokens and signing keys). -
The decoder verifies the signature by
kid, theniss(must equal the issuer),exp/nbfandaud(must list one of the configured audiences). A token issued for another audience, for example the authorization server’s own API (auth-server), is refused. -
jwtAuthenticationConvertermapsscopetoSCOPE_*authorities androlestoROLE_*authorities (BearerAuthorities). -
Authorization:
GET /api/documentsneedsSCOPE_documents.read;DELETE /api/documents/{id}goes throughStepUpAuthorizationManager(SCOPE_documents.deleteplus a recent multi-factor sign-in, see Step-up authentication); ownership is checked byDeleteDocumentUseCase. -
A token with
cnf.jktneeds a valid DPoP proof and one withcnf.x5t#S256the matching TLS client certificate.
Nothing in this repository sets or checks the RFC 9068 typ: at+jwt header; the checks that apply are the issuer,
signature, audience and lifetime ones RFC 9068 §4 also asks for.
/**
* Builds the filter chain validating bearer JWTs (the decoder is built from {@code issuer-uri}, and the
* configured audiences are validated by it).
*
* @param http the HTTP security builder.
* @param properties the resource server properties.
* @param stepUp requires a recent strong authentication to delete.
* @param accessDeniedHandler answers the denials, including the step-up challenge.
* @return the security filter chain.
* @throws Exception if the chain cannot be built.
* @throws IllegalStateException if no accepted audience is configured.
*/
@Bean
@Profile("!opaque")
SecurityFilterChain resourceServerFilterChain(final HttpSecurity http,
final OAuth2ResourceServerProperties properties, final StepUpAuthorizationManager stepUp,
final StepUpAccessDeniedHandler accessDeniedHandler) throws Exception {
requireAudiences(properties.getJwt().getAudiences());
authorize(http, stepUp);
http.oauth2ResourceServer(oauth2 -> oauth2
.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthenticationConverter()))
// DPoP (RFC 9449): "Authorization: DPoP <token>" plus a proof; the framework already refuses a
// token whose cnf holds jkt when it arrives as Bearer. A cnf.x5t#S256 token is checked against the
// TLS client certificate by the validator of the decoder Boot builds (no JwtDecoder bean here).
.dPoP(Customizer.withDefaults())
.authenticationEntryPoint(new BearerTokenAuthenticationEntryPoint())
.accessDeniedHandler(accessDeniedHandler));
return http.build();
}
/**
* Maps the {@code scope} claim to {@code SCOPE_*} authorities (the default converter) and the {@code roles}
* claim to {@code ROLE_*} authorities.
*
* @return the converter.
*/
JwtAuthenticationConverter jwtAuthenticationConverter() {
final var scopes = new JwtGrantedAuthoritiesConverter();
final var converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(jwt -> {
final var authorities = new ArrayList<GrantedAuthority>(scopes.convert(jwt));
authorities.addAll(BearerAuthorities.roleAuthorities(
jwt.getClaimAsStringList(BearerAuthorities.ROLES_CLAIM)));
return authorities;
});
return converter;
}
Introspection of reference tokens (profile opaque)
-
A client registered with
"accessTokenFormat": "reference"receives an opaque token. The authorization server stores its claims with the authorization;ReferenceTokenClaimsCustomizeraddsroles,tenantandaudto them. -
SecurityConfig.opaqueTokenFilterChain(@Profile("opaque")) hands the token toAudienceRolesOpaqueTokenIntrospector, which wraps Spring’sSpringOpaqueTokenIntrospector. -
The introspector posts
token=…toAUTH_INTROSPECTION_URI(/oauth2/introspect), authenticating withINTROSPECTION_CLIENT_IDandINTROSPECTION_CLIENT_SECRET(HTTP Basic). -
The authorization server authenticates that client and answers
activeand, for an active token,sub,client_id,scope,aud,exp,iat,roles,tenant. An unknown, expired or revoked token is{"active": false}. -
An inactive token is
401 invalid_token. For an active one,AudienceRolesOpaqueTokenIntrospectorrequiresaudto list a configured audience and addsROLE_*authorities fromroles; authorization then proceeds as for a JWT. Introspection happens on every request.
Revocation
-
The client that holds a token posts
token(and optionallytoken_type_hint=access_tokenorrefresh_token) toPOST /oauth2/revoke, authenticating as itself. A client can revoke only its own tokens. -
The server answers
200and marks the token invalid. Introspection then reports it inactive, and a revoked refresh token can no longer be used. Separately, a reused rotated refresh token revokes the whole authorization (see Refresh token rotation and reuse detection). -
A JWT-validating resource server does not ask the authorization server, so it accepts a revoked JWT until
exp; keep access token lifetimes short, or use reference tokens where immediate revocation matters.
Tokens and claims
-
Claims the resource server uses:
iss,exp,nbf,aud(validation);sub(the caller, owner of the documents);scope(SCOPE_*);roles(ROLE_*,ROLE_ADMINsees and deletes every document);acrandauth_time(step-up on delete);cnf(bound tokens). -
Introspection response:
active, plus for an active token the stored claims (sub,client_id,scope,aud,roles,tenant, …). -
RFC 6750 challenges: no token is
401withWWW-Authenticate: Bearer; an invalid token is401witherror="invalid_token"; a missing scope is403witherror="insufficient_scope". A missing step-up is401witherror="insufficient_user_authentication"(RFC 9470).
How it is tested
-
resource-server/boot:SecurityConfigIT(no token, valid token, accepted and other audience, no audience, invalid token, missing scopes, ownership, step-up on delete);OpaqueTokenSecurityIT(active, inactive, other audience, missing scope, no token, delete scope and step-up underopaque);BoundTokenIT(DPoP and certificate-bound tokens). -
auth-server/boot:TokenFlowIT(revoke_whenAccessTokenIsRevoked_introspectionTurnsInactive,revoke_whenRefreshTokenIsRevoked_itCannotBeUsed,introspect_whenTokenIsActiveOrUnknown_reportsIt,introspect_whenTokenIsAReferenceToken_returnsAudienceRolesAndTenant). -
e2e-tests:ResourceServerEndToEndITruns the packaged resource server against the packaged authorization server, in the default profile (a token for its audience accepted, one for another audience refused, no token refused) and inopaque(a reference token introspected and accepted).