liveBook · Manning

JwtAuthenticationToken

As explained in Spring Security in Action, Second Edition

JwtAuthenticationToken is a Spring Security authentication-token class representing an authenticated principal derived from a JWT.

JwtAuthenticationToken is Spring Security’s JWT-oriented authentication-token shape. Extend it when an application needs authentication data from JWT claims, such as a priority used in authorization. section 15.2

Customize the token in a short sequence:

  • Create a subclass that passes the Jwt and authorities to the superclass and stores the additional claim. section 15.2
  • Implement Converter<Jwt, CustomAuthentication> to read the claim and construct the custom token. section 15.2
  • Register the converter with the OAuth2 resource-server JWT configuration. section 15.2
  • Return Authentication from an endpoint or read it from the security context to verify the added data. section 15.2

Custom priority claim

A CustomAuthentication subclass can expose a priority field. A converter reads priority from the JWT, and the configured resource server places it on the authentication object returned by /demo. section 15.2

Listing 15.8 Defining a custom authentication object
public class CustomAuthentication 
  extends JwtAuthenticationToken {      #1

  private final String priority;        #2

  public CustomAuthentication(
    Jwt jwt,
    Collection<? extends GrantedAuthority> authorities,
    String priority) {
        
    super(jwt, authorities);
    this.priority = priority; 
  }

  public String getPriority() {
    return priority;
  }
}

Browse the Java definition of a custom JwtAuthenticationToken subclass. section 15.2

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains subclassing, conversion, configuration, and endpoint verification, but does not detail JWT validation internals.
Examples
Uses a priority claim, CustomAuthentication subclass, JWT converter, configured filter chain, and /demo response.
What it assumes
Assumes basic Spring Security and JWT resource-server configuration while defining the customization workflow concretely.
What you can do after
Build a custom JWT authentication object and configure Spring Security to populate it from access-token claims.
How it connects
Connects JWT, OAuth 2 resource servers, authentication converters, granted authorities, security contexts, and authorization.

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.