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.
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With