liveBook · Manning

Security context

As explained in Spring Security in Action, Second Edition

A security context is a Spring Security object that holds authentication information associated with the current execution or request.

A security context stores the Authentication associated with the current execution or request. After successful authentication, the filter places this data in the context, where controllers and authorization logic can use it until the action ends. section 2.2 section 6.2

Figure 6.7 After successful authentication, the authentication filter stores the details of the authenticated entity in the security context. From there, the controller implementing the action mapped to the request can access these details when needed.

Follow the request flow from successful authentication to controller access through the security context. section 6.2

Access the context through SecurityContextHolder and choose a strategy that matches the execution model:

  • MODE_THREADLOCAL keeps separate context details for each thread and suits the usual thread-per-request web application. section 6.2
  • MODE_INHERITABLETHREADLOCAL copies the context to a child thread, supporting asynchronous methods such as those annotated with @Async. section 6.2 section 6.2.2
  • MODE_GLOBAL exposes one context to every thread, which can fit standalone applications but requires care with concurrent access and synchronization. section 6.2.3

Reading authentication in an asynchronous method

An @Async method can obtain the current username with SecurityContextHolder.getContext().getAuthentication().getName(). Without an inheritable strategy, the worker thread may have no authentication and cause a NullPointerException; configure MODE_INHERITABLETHREADLOCAL when the execution model requires propagation. section 6.2.2

Method-security tests can define a SecurityContext with @WithMockUser, @WithUserDetails, or a custom annotation, then invoke the secured bean directly without MockMvc. section 18.4

How Spring Security in Action, Second Edition covers it

How deep it goes
Defines the Authentication contract and explains thread-local, inheritable, and global management strategies, including asynchronous and concurrency hazards.
Examples
Uses controller access, an @Async username lookup, strategy configuration, and direct method-security tests with authenticated and unauthorized users.
What it assumes
Assumes basic Spring Security authentication concepts and introduces how authenticated details remain available after AuthenticationManager completes.
What you can do after
Operate/configure it through SecurityContextHolder, retrieve authentication details, propagate them to asynchronous threads, and test secured methods.
How it connects
Connects authentication filters, AuthenticationManager, AuthenticationProvider, SecurityContextHolder, authorization, asynchronous methods, and method security.

Also explained in

Compare how 4 books explain security context →

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.