It depends on how you configured it (or lets say, you can configure a different behaviour).
In a Web application you will use the ThreadLocalSecurityContextHolderStrategy which interacts with SecurityContextPersistenceFilter.
The Java Doc of SecurityContextPersistenceFilter starts with:
Populates the {@link
SecurityContextHolder} with
information obtained from the
configured {@link
SecurityContextRepository} prior to
the request and stores it back in the
repository once the request has
completed and clearing the context
holder. By default it uses an {@link
HttpSessionSecurityContextRepository}.
See this class for information
HttpSession related
configuration options.
Btw: HttpSessionSecurityContextRepository is the only implementation of SecurityContextRepository (I have found in the default libs)
It works like this:
- The
HttpSessionSecurityContextRepositoryuses the httpSession (Key=”SPRING_SECURITY_CONTEXT”) to store anSecurityContextObject. - The
SecurityContextPersistenceFilteris an filter that uses anSecurityContextRepositoryfor example theHttpSessionSecurityContextRepositoryto load and storeSecurityContextObjects. If an HttpRequest passes the filter, the filter get theSecurityContextfrom the repository and put it in the SecurityContextHolder (SecurityContextHolder#setContext) - The
SecurityContextHolderhas two methodssetContextandgetContext. Both uses aSecurityContextHolderStrategyto specify what exactly is done in the set- and get-Context methods. – For example theThreadLocalSecurityContextHolderStrategyuses a thread local to store the context.
So in summary: The user principal (element of SecurityContext) is stored in the HTTP Session. And for each request it is put in a thread local from where you access it.