In addition to using interceptors and filters to jump to landing pages without access to a page, you can also implement it through the framework: Spring Security.
Use spring Security to complete login verification:
1.pom.xml adding dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-security</artifactid></dependency>
2. Create a configuration class for spring security Websecurityconfig.java
@Configuration//Open Spring Security@EnableWebSecurity Public classWebsecurityconfigextendsWebsecurityconfigureradapter {@Overrideprotected voidConfigure (Httpsecurity http)throwsException {http//define which URLs need to be protected and which are not protected through authorizerequests. Authorizerequests ()///And/users/can access. Antmatchers ("/", "/users/"). Permitall (). Anyrequest (). authenticated (). and (). Formlogin () //landing page to go to when logging in. LoginPage ("/login"). Permitall (). and (). Logout (). Permitall (); } @Autowired//create a user in memory with the user's name user1, password 123 user role admin Public voidConfigureglobal (Authenticationmanagerbuilder auth)throwsException {//get from memoryauth. Inmemoryauthentication (). Passwordencoder (NewBcryptpasswordencoder ()). Withuser ("User1"). Password (NewBcryptpasswordencoder (). Encode ("123"). Roles ("Admin"); }}
New Login Request and page
Logincontroller.java
@Controller Public class Logincontroller { @RequestMapping ("/login") public String Login () { return "Login"; }}
Resources/templates/login.html
<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"xmlns:th= "http://www.thymeleaf.org"xmlns:sec= "Http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"><Head> <title>Spring Security Example</title></Head><Body><Divth:if= "${param.error}">User name or password error</Div><Divth:if= "${param.logout}">you have logged off successfully</Div><formth:action= "@{/login}"Method= "POST"> <Div><label>User name:<inputtype= "text"name= "username"/> </label></Div> <Div><label>Password<inputtype= "Password"name= "Password"/> </label></Div> <Div><inputtype= "Submit"value= "Login"/></Div></form></Body></HTML>
Test: Access/users/no problem, access/USERS/2 jump to login.html page, enter User1 pass:123 then login successfully, no problem in Access/USERS/2.
Spring Boot (23) using Redis