Reputation: 455
I get the currently logged in user by
SecurityContextHolder.getContext().getAuthentication()
in server side and do some logging on users.
Here is the question:
Suppose I have three user logged in.
How the server side can identify the user just simply calling SecurityContextHolder.getContext().getAuthentication();
?
Thanks for your reply.
Upvotes: 6
Views: 3118
Reputation: 7817
By default there are 3 important things here:
SecurityContextHolder
before each request from HTTP session (and stores authentication object back once the request has completed)ThreadLocal
- stores authentication object during request processingAfter authentication corresponding SecurityContext
object is stored in HTTP session.
Before each request processing special SecurityContextPersistenceFilter
is fired. It is responsible for loading of SecurityContext
object from HTTP session (via SecurityContextRepository
instance) and for injecting SecurityContext
object into SecurityContextHolder
. Take a look at the source code of SecurityContextPersistenceFilter
class for more details. Another important part is that by default SecurityContextHolder
stores SecurityContext
object using ThreadLocal
variable (so you will have a different authentication object per thread).
EDIT. Additional questions:
SecurityContextHolder
is not an instance, it is a helper class with static methods.SecurityContext
is stored in ThreadLocal
variable. SecurityContextHolder
is a helper class that may be used to get/set SecurityContext
instance via ThreadLocal
variable.SecurityContextHolder
used by all threads to get/set corresponding SecurityContext
.ThreadLocal
variable has different values for different threads.Upvotes: 9
Reputation: 905
For every logged-in
user, there will be different sessions. Every session
have its own configuration. Therefore, at server side, SecurityContext
load data specific to a session
. You can visualise data in SecurityContext
as a map
(key-value) pair.
Upvotes: 0