Quick revision
The short answer
SecurityContextHolder keeps the current SecurityContext, which holds the Authentication, in a ThreadLocal by default (MODE_THREADLOCAL), so each thread sees only its own user. The other strategies are MODE_INHERITABLETHREADLOCAL, which copies the context to child threads when they are created, and MODE_GLOBAL, which shares one context across the whole JVM. An @Async method runs on a pool thread that never had the caller's ThreadLocal, so it sees no user; we fix it by wrapping the executor with DelegatingSecurityContextAsyncTaskExecutor, which copies the context into each task.
What to remember
In this answer, we saw that SecurityContextHolder keeps the SecurityContext in a ThreadLocal by default, which gives each request thread its own user. The inheritable strategy copies the context to new child threads and the global strategy shares one context for the JVM. An @Async method runs on a pool thread that never had the caller's ThreadLocal, so it sees no user. The fix is to wrap the executor with DelegatingSecurityContextAsyncTaskExecutor or apply a TaskDecorator, which copies the context into each task and restores the thread afterwards, while the inheritable strategy is unsafe with pools because reused threads keep stale contexts.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
SecurityContextHolder keeps the current SecurityContext, which holds the Authentication, in a ThreadLocal by default (MODE_THREADLOCAL), so each thread sees only its own user. The other strategies are MODE_INHERITABLETHREADLOCAL, which copies the context to child threads when they are created, and MODE_GLOBAL, which shares one context across the whole JVM. An @Async method runs on a pool thread that never had the caller's ThreadLocal, so it sees no user; we fix it by wrapping the executor with DelegatingSecurityContextAsyncTaskExecutor, which copies the context into each task.
Why this question is asked
Sooner or later every Spring application moves some work off the request thread: sending an email, calling a slow service, writing an audit record. And then a method that worked perfectly in the controller suddenly finds no logged-in user, or worse, finds the wrong one. Interviewers ask this because the answer shows whether we know what a ThreadLocal is, why Spring Security uses one, and why the obvious fix with inheritance is a trap. In this answer, we will recap what SecurityContextHolder holds, look at the three strategies, reproduce the @Async problem, fix it properly, and see why the inheritable strategy is unsafe with thread pools.
1A Quick Recap of SecurityContextHolder
When a request is authenticated, Spring Security creates an Authentication object: who the user is (the principal), what they may do (the authorities) and whether they are authenticated. It wraps it in a SecurityContext, and stores that context in SecurityContextHolder, a class with static methods that any code can call to ask who the current user is.
Static methods need somewhere to keep per-request data, because many requests run at the same time. The answer is a ThreadLocal, a variable that holds a separate value for each thread. A servlet container handles each request on one thread from start to finish, so a ThreadLocal is a natural home for the current user: the request thread sets it, every method called on that thread reads it, and FilterChainProxy clears it at the end.
2The Three Storage Strategies
How the holder stores the context is decided by a strategy, a SecurityContextHolderStrategy implementation chosen by name. We can set it with the system property spring.security.strategy or by calling SecurityContextHolder.setStrategyName() once at startup, before any context is stored.
| Strategy | Where the context lives | Good for |
|---|---|---|
MODE_THREADLOCAL (default) | A ThreadLocal: one context per thread | Servers; every request on its own thread |
MODE_INHERITABLETHREADLOCAL | Copied to a child thread when it is created | Short-lived threads we create ourselves |
MODE_GLOBAL | One static field for the whole JVM | Single-user desktop applications only |
MODE_THREADLOCAL is the default and the right choice for almost every application. MODE_INHERITABLETHREADLOCAL uses an InheritableThreadLocal, which copies the parent thread's value into a new thread at the moment the new thread is created; note that it copies the reference, so parent and child share one SecurityContext object. MODE_GLOBAL keeps a single static context for the whole JVM, which only makes sense in a desktop application with one user, and would be a serious bug on a server, because every request would see the same user.
3Reproducing the @Async Problem
@Async tells Spring to run a method on another thread through a proxy. The proxy submits the call as a task to a TaskExecutor, a thread pool, and returns at once. In Spring Boot the default is the auto-configured applicationTaskExecutor.
6 messages in order. Play them, or step through them one at a time.
The ThreadLocal belongs to a thread, and the task changed threads.
Notice that the controller sees alice and the async method sees null. The reason is that task-1 is a pool thread. It was created long before this request, by nobody in particular, and our request thread never wrote anything into task-1's ThreadLocal. Each thread has its own slot, and task-1's slot is empty.
4Fix: Copy the Context Into Each Task
The fix is to capture the caller's context when the task is submitted and set it on the pool thread while the task runs. Spring Security provides wrappers that do exactly this. DelegatingSecurityContextAsyncTaskExecutor wraps an AsyncTaskExecutor, DelegatingSecurityContextExecutor wraps a plain Executor, and both wrap each Runnable in a DelegatingSecurityContextRunnable.
Notice what the wrapper does around each task. When the task is submitted, it captures the current SecurityContext. On the pool thread, it saves whatever context that thread had, sets the captured one, runs the task, and restores the previous context in a finally block. That last step matters: the pool thread goes back to the pool clean, so the next task does not inherit alice.
If we define our own executor bean, Boot's auto-configured one backs off, so we take over its configuration. With the TaskDecorator approach we keep Boot's executor and its spring.task.execution properties, and only add the propagation.
5Why INHERITABLETHREADLOCAL Is Unsafe With Pools
It is tempting to switch the strategy to MODE_INHERITABLETHREADLOCAL instead, because then new threads get the parent's context automatically. But the copy happens only when a thread is created, and a pool creates its threads once and reuses them for thousands of tasks.
Picture the sequence. Alice's request is the first to use the pool, so the pool creates task-1 on Alice's request thread, and task-1 inherits Alice's context. Later Bob's request submits a task, the pool reuses task-1, and the task runs as Alice. The pool thread keeps that stale context for its whole life. With the shared reference, a change on one side can even appear on the other. So the inheritable strategy turns a visible bug (no user) into a hidden one (the wrong user), which is far worse for security and auditing.
6Follow-up questions
Does the same problem happen with CompletableFuture.supplyAsync()?
Yes. Without an executor argument it runs on ForkJoinPool.commonPool(), whose threads have no security context. We pass an executor wrapped with DelegatingSecurityContextExecutor as the second argument, so each stage that runs on it gets the caller's context.
Does the async task see changes the caller makes to the context afterwards?
No. The wrapper captures the SecurityContext at submit time, and Spring Security's own code replaces the context with a new object rather than changing it in place. The task sees the user as it was when the work was handed over, which is usually what we want for auditing.
Is MODE_GLOBAL ever used on a server?
No. It stores one context in a static field, so every request on every thread would see whichever user logged in last. It exists for standalone desktop clients with exactly one user.
7Conclusion
In this answer, we saw that SecurityContextHolder keeps the SecurityContext in a ThreadLocal by default, which gives each request thread its own user. The inheritable strategy copies the context to new child threads and the global strategy shares one context for the JVM. An @Async method runs on a pool thread that never had the caller's ThreadLocal, so it sees no user. The fix is to wrap the executor with DelegatingSecurityContextAsyncTaskExecutor or apply a TaskDecorator, which copies the context into each task and restores the thread afterwards, while the inheritable strategy is unsafe with pools because reused threads keep stale contexts.
Open with
SecurityContextHolder holds a SecurityContext, which wraps the Authentication, and by default it stores it with MODE_THREADLOCAL, so each thread has its own value. That fits servlet apps because one request runs on one thread, and FilterChainProxy clears the value at the end.
Then, if they want more
There are two other strategies. MODE_INHERITABLETHREADLOCAL copies the context into a child thread at the moment the thread is created, and MODE_GLOBAL keeps one context for the whole JVM, which only makes sense for a single-user desktop app.
An @Async method sees no user because the proxy hands the call to a thread pool, and the pool thread was created long before the request; the caller's ThreadLocal simply is not on that thread. The fix is to propagate the context per task.
I'd wrap the executor with DelegatingSecurityContextAsyncTaskExecutor, or in Boot add a TaskDecorator that wraps each Runnable in DelegatingSecurityContextRunnable. The wrapper captures the context when the task is submitted, sets it on the pool thread while the task runs, and restores the old context afterwards so the thread goes back clean.
I would not switch to the inheritable strategy, because pools reuse threads: the first request that triggers a thread's creation leaves its user on that thread, and later tasks for other users run as that person. That turns a missing user into a wrong user.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how SecurityContextHolder stores the current authentication, the available...": https://scrivio-interview-library.netlify.app/spring-boot/security-architecture/27824000ef/