liveBook · Manning

Custom authentication object

As explained in Spring Security in Action, Second Edition

A custom authentication object is an application-defined authentication-token subtype that carries identity, authorities, and additional authentication data.

A custom authentication object is an application-defined authentication-token subtype that preserves identity and authorities while adding data needed for authorization. For JWT resource servers, extend JwtAuthenticationToken and expose fields such as priority through the SecurityContext. section 15.2

Custom authentication in a test

Tests can construct a specific Authentication type by defining a custom annotation, implementing WithSecurityContextFactory, linking them with @WithSecurityContext, and applying the annotation to a MockMvc test. section 18.3

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains subtype definition, claim conversion, resource-server configuration, endpoint verification, and test factories without detailing authentication internals.
Examples
Uses a priority claim, CustomAuthentication, a /demo endpoint, @WithCustomUser, and a factory-backed authenticated MockMvc test.
What it assumes
Assumes basic Spring Security, JWT, SecurityContext, and testing knowledge while demonstrating custom claims and authentication construction explicitly.
What you can do after
Build it from scratch by extending JwtAuthenticationToken, converting JWT claims, configuring the converter, and creating matching test contexts.
How it connects
Connects JWT, OAuth 2 resource servers, JWT authentication converters, SecurityContext, AuthenticationProvider, custom annotations, and MockMvc.

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.