Spring Security4 source code to explore the permission mechanism, springsecurity4
We know that springSecurity will obtain the role permissions of a user upon logon. You are a common user and may not have the administrator permissions. After a user logs on, Authentication obtains the user's permissions. Users cannot log on to the system to generate their own Authentication.
So what is the existence of this Authentication? Server? How does the system differentiate which Authentication is used when all 1 million users log on at the same time?
Test. Use two accounts to log on to different browsers. One is Firefox and the other is Google. The console prints
========================================= Two roles do not interfere with each other.
This authorization is verified when the logon interface is called.
My question is how to identify the user and role in the memory.
Other interfaces are not authorized when AuthUtil is used to obtain permissions.
Is there a client cookie?
Now, a test browser is opened in two windows. log on to two different accounts and different roles have different permissions.
Let's get started with the source code.
First look at my custom authorization interface:
Returns a UsernamePasswordAuthenticationToken object that stores the user name. Password. Permission
Click in
Top-level interface AbstractAuthenticationToken
Click in
AbstractAuthenticationToken is an abstract class that implements two interfaces: Authentication // CredentialsContainer
Click the CredentialsContainer interface.
CredentialsContainer does Google Translate creden are all authorizations stored here? There is only one method, void eraseCredentials (); clear the credential
================================== Do not explore
Check another API Authentication.
Implements the Serializable interface. Serialized object.
Well, I still can't understand it. That's another way to get permissions, not AuthUtil.
Check the SecurityContextHolder source code.
There is a static method initialize ();
Check this method.
The first if statement returns an empty value.
StrategyName = MODE_THREADLOCAL;
THREADLOCAL Thread Local variable
Each thread can change its own copy independently without conflict with the copies of other threads.
Continue to look at the following:
Jump here:
Returns a ThreadLocalSecurityContextHolderStrategy object.
Declare a ThreadLocal
Objects are stored.
The permission is saved here.
======================================
Focus on this set method to see which calls
Called by seven methods:
Slot,
First
Check whether the abstract permission filter of the parent class AbstractSecurityInterceptor is stored at this time.
Second
DelegatingSecurityContextCallable delegates the permission context object, and does not look like it.
Third
Google Translate Is a digest authorization filter, which should not be stored at this time
Fourth
Permission context persistence Filter
When you see persistence, click =
Take it in response? I remember that I did not throw my authorization to response.
Continue to check the five
Abstract permission Interceptor. This is not the case anymore.
Sixth
Context process interceptor. Maidan is not.
Seventh
Delegate permission context Interface
This thread does not seem to be, and it seems confused now.
Okay, let's take a look at UsernamePasswordAuthenticationToken.
Finally, I found it. Okay, this is the final setting method.
Let's look at his source code:
This is the case.
========================================================== ========================================================== ====================================
Now I understand the principle:
1. Verify the user name and password.
2. If the authorization is passed, it will be placed in threadLocal.
Q: How can a user determine that a user is the user by calling a method and obtaining a method?
Multiple users call this code on the server to obtain different roles !!!!
ThreadLocal plays an important role in Spring. It has appeared in Bean, transaction management, task scheduling, AOP and other modules that manage request scopes, and plays an important role. To understand the underlying technology of Spring transaction management, ThreadLocal is a mountain fortress that must be overcome.
What is ThreadLocal?
As early as JDK 1.2, java. lang. ThreadLocal is provided. ThreadLocal provides a new way of thinking to solve the concurrent problem of multi-threaded programs. Using this tool class, you can easily compile beautiful multi-threaded programs.
ThreadLocal is easy to understand and take for granted as a "local thread ". In fact, ThreadLocal is not a Thread, but a local variable of the Thread. It may be easier to understand to name it ThreadLocalVariable.
When ThreadLocal is used to maintain a variable, ThreadLocal provides an independent copy of the variable for each thread that uses the variable. Therefore, each thread can change its own copy independently, it does not affect the copies corresponding to other threads.
========================================================== ================= Too many source code, some people say that reading source code is a kind of enjoyment, so few