Spring Boot official provides a clear and understandable example of landing authentication,
In this example, we will demonstrate how spring boot can be certified for login.
First we go to https://start.spring.io/download a sample project Spring-test.
Our example contains three pages, which are placed in the src/main/resources/templates/directory:
- Landing page: login.html
- Home: home.html
- Welcome page: hello.html
Consists of three classes:
- Main class: Springtestapplication.java
- MVC mapping Relationship class: Mvcconfig.java
- Security Control class: Websecurityconfig.java
Related maven dependencies:
Pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency > <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-w Eb</artifactid> </dependency> <!--tag::security[]--<dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-security</ar Tifactid> </dependency> <!--end::security[]-<dependency> <gro Upid>org.springframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId> Org.springframework.secuRity</groupid> <artifactId>spring-security-test</artifactId> <scope>test< /scope> </dependency> </dependencies>
Mvcconfig.java
package com.zifeiy.springtest.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class MvcConfig implements WebMvcConfigurer { public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/home").setViewName("home"); registry.addViewController("/").setViewName("home"); registry.addViewController("/hello").setViewName("hello"); registry.addViewController("/login").setViewName("login"); }}
This class is primarily used to configure the URL and view correspondence.
Websecurityconfig.java
Package Com.zifeiy.springtest.config;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.security.config.annotation.web.builders.httpsecurity;import Org.springframework.security.config.annotation.web.configuration.enablewebsecurity;import Org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter;import Org.springframework.security.core.userdetails.user;import Org.springframework.security.core.userdetails.userdetails;import Org.springframework.security.core.userdetails.userdetailsservice;import Org.springframework.security.provisioning.InMemoryUserDetailsManager, @Configuration @enablewebsecuritypublic Class Websecurityconfig extends Websecurityconfigureradapter {@Override protected void Configure (Httpsecurity http) Throws Exception {http. authorizerequests (). Antmatchers ("/", "/Home"). Permitall () . Anyrequest (). auThenticated (). and (). Formlogin (). LoginPage ("/login"). Permitall ( ). and (). Logout (). Permitall (); } @Bean @Override public userdetailsservice userdetailsservice () {userdetails user = User.wit Hdefaultpasswordencoder (). Username ("user"). Password ("password"). Roles ("Use R "). Build (); return new Inmemoryuserdetailsmanager (user); }}
which
http .authorizeRequests() .antMatchers("/", "/home").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll();
The meaning of the expression is:
Allow all users to access "/" and "/home";
Via form upload form, the URL of the form is "/login";
Allow all users to log out (logout).
But all the other URLs are not directly accessible, unless logged in, such as the "/hello" link here, you can not directly login.
The userdetails here is used to set the login user name, password is password when the correct landing.
Login.html
<!DOCTYPE html>
Home.html<!DOCTYPE html>
Hello.html<!DOCTYPE html>
Spring boot ensures Web application security (login authentication)