In Spring Security, SecurityContextHolder is where the framework keeps the details of the user making the current request. It wraps a SecurityContext, which in turn holds an Authentication with the user's principal, authorities, and authentication state. Filters write to it after login, and authorization checks and your own code read from it.
Its purpose is to make the current user available anywhere in the call stack of a request without passing it through every method.
Three types work together:
| Type | Role |
|---|---|
SecurityContextHolder | Static accessor that finds the context for the running thread |
SecurityContext | Container with a single Authentication |
Authentication | Principal, credentials, authorities, and an authenticated flag |
After a successful login, an AuthenticationManager returns a fully populated Authentication, and the filter chain places it in the context.
Inside a controller, @AuthenticationPrincipal or a Principal parameter is simpler and easier to test, so use the holder directly in services or utilities that have no other access to the request.
For servlet applications the flow is:
SecurityContext from a SecurityContextRepository, for example the HTTP session, or builds it from a token such as a JWT.SecurityContextHolder for that thread.If you authenticate manually, for example in a custom login endpoint, set the context and save it explicitly, since Spring Security 6 no longer saves it for you:
Here securityContextRepository is typically an HttpSessionSecurityContextRepository bean.
The context is also what authorization reads. When a request reaches authorizeHttpRequests rules or a method annotated with @PreAuthorize, Spring Security takes the Authentication from the holder and compares its authorities with the required ones. Because the same data feeds URL rules, method security, and your own code, you rarely need to pass the user around manually.
A custom check can use the same source. For example, an owner check inside a service reads auth.getName() and compares it with the record's owner before allowing a change, and throws AccessDeniedException when they differ.
Spring Security Test populates the holder for you:
@WithMockUser installs a context before the test and clears it afterwards, so tests stay independent.
CompletableFuture without propagation. The value will be missing.getAuthentication() != null as proof of login, because anonymous requests carry an anonymous token.ReactiveSecurityContextHolder there.Typically an AnonymousAuthenticationToken with the authority ROLE_ANONYMOUS, as long as anonymous authentication is enabled.
Yes, as long as the code runs on the request thread. For other threads, propagate the context with a delegating executor.
Call SecurityContextLogoutHandler().logout(request, response, authentication), which clears the context and invalidates the session.
Usually yes in controllers, since it keeps methods easier to test. Use the holder where injection is not available.
SecurityContextHolder gives code on the current thread access to the authenticated user's SecurityContext. The framework fills it during the filter chain and clears it afterwards. Read it for identity and authorities, set it with a new context when you authenticate manually, and carry it explicitly to other threads.