liveBook · Manning

OAuth2TokenCustomizer

As explained in Spring Security in Action, Second Edition

OAuth2TokenCustomizer is a Spring Authorization Server callback interface for modifying OAuth 2 token claims or other token attributes before issuance.

OAuth2TokenCustomizer lets an authorization server add application-specific claims to OAuth 2 access tokens before issuance. A resource server can then read those claims and use them in authorization decisions. section 15.2

@Bean
public OAuth2TokenCustomizer<JwtEncodingContext> jwtCustomizer() {
 return context -> {
   JwtClaimsSet.Builder claims = context.getClaims();
   claims.claim("priority", "HIGH");
 };
}

Define a JwtEncodingContext customizer, obtain the claims builder, and add a claim such as priority to the token. section 15.2

A practical integration proceeds in three stages:

  • Customize the authorization server so issued JWT access tokens contain the application-specific claim. section 15.2
  • Configure the resource server to convert the claim into a custom authentication object. section 15.2
  • Apply the authentication value in authorization rules; an endpoint can expose the resulting authentication for verification. section 15.2

How Spring Security in Action, Second Edition covers it

How deep it goes
Traces bean definition, JWT inspection, claim conversion, authentication configuration, and endpoint verification without developing claim-setting business logic.
Examples
Uses a priority claim with value HIGH, a decoded JWT body, a custom authentication class, and a /demo endpoint response.
What you can do after
Apply it via a Spring bean to customize JWT claims and connect those claims to resource-server authorization.
How it connects
Connects JWT access tokens with resource servers, custom claims, authentication objects, JWT converters, and authorization rules.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.