SecurityContextHolder
About
SecurityContextHolder
is a utility class in Spring Security that provides access to the SecurityContext
. It is responsible for storing and retrieving authentication details of the currently authenticated user. It plays a important role in Spring Security’s authentication and authorization process by maintaining security information throughout the lifecycle of a request.
Why is SecurityContextHolder Important?
Centralized access to security details – Provides access to the currently authenticated user from anywhere in the application.
Manages security context per thread – Ensures thread-local storage of authentication details.
Facilitates method-level security – Used to enforce role-based access.
Allows customization of storage strategies – Supports different ways to store security context data.
SecurityContextHolder Class Overview
The SecurityContextHolder
class is a final class that provides static methods for managing the security context.
Method
Purpose
getContext()
Retrieves the current SecurityContext
.
setContext(SecurityContext context)
Manually sets the security context.
clearContext()
Clears authentication details (used on logout).
setStrategyName(String strategyName)
Changes the storage strategy (default is ThreadLocal)
SecurityContext Storage Strategies
Spring Security provides three different strategies for storing SecurityContext
:
Strategy
Description
Usage
ThreadLocal (Default)
Stores SecurityContext
in the current thread.
Default strategy, used in web apps.
InheritableThreadLocal
Allows child threads to inherit SecurityContext
.
Useful for async processing.
Global (Shared Context)
A single SecurityContext
shared across all threads.
Not recommended due to security risks.
Changing SecurityContext Strategy
We can change the default ThreadLocal
storage to MODE_INHERITABLETHREADLOCAL
:
Clearing SecurityContext on Logout
To clear the security context on logout, we can use:
Prevents unauthorized access after logout
How to Use SecurityContextHolder
1. Accessing SecurityContext in Controllers
Spring Security provides SecurityContextHolder
to retrieve authentication details.
Retrieves the currently logged-in user's username.
authentication.getAuthorities()
can be used to fetch roles/permissions.
2. Accessing SecurityContext in Services
If we need user details inside a service layer:
Ensures authentication details are available throughout the request lifecycle.
3. Setting SecurityContext Manually
If we need to set authentication manually (e.g., for programmatic login):
This is useful for custom authentication mechanisms.
SecurityContextHolder Configuration in Spring Boot
Spring Boot 2 – SecurityContext Configuration
Before Spring Security 5.7+, configurations were done via WebSecurityConfigurerAdapter
:
Uses
SecurityContextHolder.clearContext()
on logout.
Spring Boot 3 – SecurityContext Configuration
Spring Boot 3 removes WebSecurityConfigurerAdapter
and uses bean-based configuration:
Uses lambda-based DSL for cleaner security configuration.
Last updated
Was this helpful?