II. Summary statement
- This article mainly introduces the login and logout business processes, so the use of memory-based user names and passwords, the authorization of the relevant content is not introduced, followed by a detailed introduction to the database-based authentication and authorization
- How to View Memory-Based Default Username Passwords
- How to Configure Memory-Based Custom Username Passwords
- This article is strongly related to the above, so if you are unfamiliar with the filters related to logging in the filter chain, please first check theSpring Boot integration Spring Security security securityFilterChain filter chain details
III. Memory-based username and password
1. Default user name and password
- I. Spring Boot integration Spring Security of the auto-assemblySection 6 of this document describes how to generate the default authentication interface inMemoryUserDetailsManager when the user does not customize the authentication interface, where the user name is user and the password is a randomly-generated uuid, which is printed on the console when the project is started.
- Username and password source code
2、Customize user name and password
- Configuration binding in the previous subsection [username password source code] can be configured to customize the username, password
- Configure username and password via configuration file
3, to facilitate the test to add a test interface TestController
package ;
import ;
import ;
import ;
@RestController
@RequestMapping("/test")
public class TestController {
@GetMapping("/hello")
public String hello() {
return "success";
}
}
IV. Introduction to important concepts of login and logout
- Security context repository (SecurityContextRepository): used to store the security context, the default session-based implementation (HttpSessionSecurityContextRepository)
- SecurityContextHolder: Used to store the security context of the request, based on the ThreadLocal implementation by default.
- Security context (SecurityContext): used to store authentication information
- Authentication information (Authentication): used to store the user and authentication results information, the main implementation classes are
- UsernamePasswordAuthenticationToken: UsernamePasswordAuthenticationToken
- Anonymous Authentication Token: AnonymousAuthenticationToken
- Login page request: a request to jump to the login page
- Login request: a request submitted after entering a username and password on the login page
- Logout page request: request to jump to the logout page
- Logout request: Confirmation of logout submission on the logout page
V. Login Business Logic
1. Login to business-related filters
- SecurityContextPersistenceFilter
- UsernamePasswordAuthenticationFilter
- DefaultLoginPageGeneratingFilter
- AnonymousAuthenticationFilter
- ExceptionTranslationFilter
- FilterSecurityInterceptor
2. Access to the business request processing flow
1)、Access to the business request address is intercepted and redirected to the login page request
-
Browser access to the business request address:http://localhost:8080/test/hello
-
SecurityContextPersistenceFilter handles the request:
- Get security context from security context repository as empty, create security context without authentication information (SecurityContextImpl)
- Set the security context obtained in step 1 to the security context holder
- Execution of subsequent filter chains
- source code (computing)
-
AnonymousAuthenticationFilter handles the request:
- Get authentication information in the security context in the security context holder is empty
- Creating Anonymous Authentication Information
- Creating a new security context with no authentication information
- Set the anonymous authentication information in step 2 to the security context in step 3
- Set the security context in step 3 to the security context holder
- Execution of subsequent filter chains
- source code (computing)
-
FilterSecurityInterceptor handles the request:
- Verify that the anonymous authentication information in the security context in the security context holder passes the
- Failure to validate authorization information (when the business request address is not set to be accessed anonymously), an AccessDeniedException is thrown.
- source code (computing)
-
ExceptionTranslationFilter handles the request:
- Catch AccessDeniedException thrown by FilterSecurityInterceptor.
- Determining authorization exceptions due to anonymous access
- Creating a new security context with no authentication information
- Set the security context in step 3 to the security context holder
- Redirects to the login page:http://localhost:8080/login
- source code (computing)
-
SecurityContextPersistenceFilter handles the request:
- Code after execution
- Get the security context in the security context holder
- Deleting a Security Context in a Security Context Holder
- Save the security context obtained in step 2 to the security context repository
- source code (computing)
-
Redirects login page requests:http://localhost:8080/login(GET)
2)、Redirect the fixed page request, return to the login page
-
SecurityContextPersistenceFilter handles the request:
- Get security context from security context repository as empty, create security context without authentication information (SecurityContextImpl)
- Set the security context obtained in step 1 to the security context holder
- Execution of subsequent filter chains
- Source code (ibid.)
-
DefaultLoginPageGeneratingFilter handles the request:
- Determine if the request is a jump to the login page
- Generate default login page
- Returns and renders the generated default login page
- source code (computing)
-
SecurityContextPersistenceFilter handles the request:
- Code after execution
- Get the security context in the security context holder
- Deleting a Security Context in a Security Context Holder
- Save the security context obtained in step 2 to the security context repository
- Source code (ibid.)
3), enter the correct user name and password, redirect to the business request
-
SecurityContextPersistenceFilter handles the request:
- Get security context from security context repository as empty, create security context without authentication information (SecurityContextImpl)
- Set the security context obtained in step 1 to the security context holder
- Execution of subsequent filter chains
- Source code (ibid.)
-
UsernamePasswordAuthenticationFilter handles the request:
- Determining the need for authentication (method)
- Authentication username password successful, generate authenticated authentication information UsernamePasswordAuthenticationToken
- Creating a new security context with no authentication information
- Set the authentication information in step 2 to the security context in step 3
- Set the security context from step 3 to the security context holder
- Save the security context from step 3 to the local variable security context repository (empty implementation)
- Redirects to the business request address:http://localhost:8080/test/hello
- source code (computing)
-
SecurityContextPersistenceFilter handles the request:
- Code after execution
- Get the security context in the security context holder
- Deleting a Security Context in a Security Context Holder
- Save the security context obtained in step 2 to the security context repository
- Source code (ibid.)
4), redirection to business requests
-
SecurityContextPersistenceFilter handles the request:
- Get authenticated security contexts from the security context repository
- Set the security context obtained in step 1 to the security context holder
- Execution of subsequent filter chains
- Source code (ibid.)
-
FilterSecurityInterceptor handles the request:
- Verify that the authentication information in the security context in the security context holder passes the
- Verify that the authorization was successful
- Calling the interface to return data
- source code (computing)
-
SecurityContextPersistenceFilter handles the request:
- Code after execution
- Get the security context in the security context holder
- Deleting a Security Context in a Security Context Holder
- Save the security context obtained in step 2 to the security context repository
- Source code (ibid.)
VI. Logic for logout implementation
1. Logging out of business-related filters
- SecurityContextPersistenceFilter
- LogoutFilter
- DefaultLogoutPageGeneratingFilter
2、Access to the logout page request processing flow
-
Browser access to logout request address:http://localhost:8080/logout
-
SecurityContextPersistenceFilter handles the request:
- Get authenticated security contexts from the security context repository
- Set the security context obtained in step 1 to the security context holder
- Execution of subsequent filter chains
- Source code (ibid.)
-
DefaultLogoutPageGeneratingFilter handles the request:
- Determine if the request is a jump to a logout page
- Generate default logout page
- Returns and renders the generated default logout page
- source code (computing)
-
SecurityContextPersistenceFilter handles the request:
- Code after execution
- Get the security context in the security context holder
- Deleting a Security Context in a Security Context Holder
- Save the security context obtained in step 2 to the security context repository
- Source code (ibid.)
3、Logout page to confirm the logout request processing flow
1)、Confirm logout, redirect to login page request
-
SecurityContextPersistenceFilter handles the request:
- Get authenticated security contexts from the security context repository
- Set the security context obtained in step 1 to the security context holder
- Execution of subsequent filter chains
- Source code (ibid.)
-
LogoutFilter handles the request:
- Judgment is a logout request
- Get the security context in the security context holder
- Logout processor handles logout operations
- Deleting a Security Context in a Security Context Holder
- Create a security context with no authentication information
- Save the security context from step 2 to the security context repository
- Redirect to login page
- source code (computing)
-
SecurityContextPersistenceFilter handles the request:
- Code after execution
- Get the security context in the security context holder
- Deleting a Security Context in a Security Context Holder
- Save the security context obtained in step 2 to the security context repository
- Source code (ibid.)
2)、Login page request
The logon business logic is described in Section V and will not be repeated.
VII. Description
- Spring Boot integration with Spring Security is a non-front-to-back architecture by default.
- The process described in this article is a non-front-to-back split version of the processing flow
- Simpler front-to-back processing
- Accessing the business interface without authentication returns an unauthenticated error message
- Returns a Token after a successful call to the login interface, which is carried in the request header thereafter
- A successful call to the logout interface returns success and the backend invalidates the Token
- Carry the Token to access the business interface, and after the back-end verifies the Token successfully, call the business interface and return the data