SecurityContextHolder is the Spring Security class that stores the SecurityContext of the current execution, and that context holds the Authentication object describing the signed-in user. Anywhere in your code you can call SecurityContextHolder.getContext().getAuthentication() to see who is making the request, what authorities they have and whether they are authenticated.
By default it keeps this data in a ThreadLocal, so each thread, and therefore each request in a servlet application, sees its own user.
| Type | Role |
|---|---|
SecurityContextHolder | Static access point; delegates to a storage strategy |
SecurityContext | Holder for the current Authentication |
Authentication | Principal, credentials and granted authorities of the user |
GrantedAuthority | One permission or role, such as ROLE_ADMIN |
During a web request, the security filter chain loads the context (from the HTTP session or another SecurityContextRepository) into the holder, runs the rest of the chain and the controller, and then clears it so the thread can be reused safely.
For anonymous visitors Spring Security usually places an AnonymousAuthenticationToken in the context, so also check that the token is not anonymous when that matters.
In controllers you rarely need the static call. Let Spring inject the value:
Injected parameters are easier to test than static access, so prefer them in web layers and use the holder in code that has no other way to reach the user.
Typical cases are custom login endpoints, tests and background jobs. Create a new context instead of mutating the one already stored, which avoids race conditions between threads.
In Spring Security 6 and later, the framework no longer saves the context to the session automatically after you set it yourself. In a custom login controller, also persist it:
To log out programmatically, call SecurityContextHolder.clearContext() and invalidate the session, or use SecurityContextLogoutHandler.
The strategy decides how the context is stored. Set it with SecurityContextHolder.setStrategyName(...) or the spring.security.strategy system property.
| Strategy | Behaviour |
|---|---|
MODE_THREADLOCAL (default) | One context per thread |
MODE_INHERITABLETHREADLOCAL | Child threads copy the parent context at creation |
MODE_GLOBAL | One context for the whole JVM, suitable only for standalone clients such as desktop apps |
Never use MODE_GLOBAL on a server, because all users would share one identity.
Because the default is thread-bound, new threads do not see the user. This affects @Async, executors and CompletableFuture. The fix is to wrap the executor or task:
Use DelegatingSecurityContextRunnable or DelegatingSecurityContextCallable for single tasks, and DelegatingSecurityContextAsyncTaskExecutor for Spring's @Async infrastructure. With virtual threads, the ThreadLocal still belongs to the individual thread, so the same wrapping is needed for tasks you hand to another thread.
Reactive applications do not use ThreadLocal. Spring Security stores the context in the Reactor Context and exposes it through ReactiveSecurityContextHolder:
Yes in the default mode, because each thread has its own SecurityContext. The risk is leaking a context between requests if code sets it and never clears it, so clear it when you manage it yourself outside the filter chain.
The async task runs on another thread that has no copy of the context. Use a DelegatingSecurityContextAsyncTaskExecutor or pass the needed user data as an argument.
Principal is the standard Java interface with only getName(). Authentication extends it and adds authorities, credentials, details and the authenticated flag.
Annotate the test with @WithMockUser, or set a context manually and clear it in an @AfterEach method.
SecurityContextHolder gives code access to the current SecurityContext and its Authentication, stored per thread by default. Prefer injected parameters in controllers, create new contexts when you set them manually, propagate them explicitly to other threads, and clear them when you own the lifecycle.