For users who do not have access rights, they need to go to the Login form page. There are various ways to implement access control, either through AOP, interceptors, or through frameworks (e.g. Apache Shiro, Spring Security).
Pom.xml adding dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Creating the Springsecurity Configuration class
Importorg.springframework.beans.factory.annotation.Autowired;
ImportOrg.springframework.context.annotation.Configuration;
Import
Org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
Importorg.springframework.security.config.annotation.web.builders.HttpSecurity;
Importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Import
Org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
Public classWebsecurityconfigextendsWebsecurityconfigureradapter {
@Override
protected voidConfigure (Httpsecurity http)throwsException {
http
. Authorizerequests ()
. Antmatchers ("/", "/Home"). Permitall ()
. Anyrequest (). Authenticated ()
. and ()
. Formlogin ()
. LoginPage ("/login")
. Permitall ()
. and ()
. Logout ()
. Permitall ();
}
@Autowired
Public voidConfigureglobal (Authenticationmanagerbuilder auth)throwsException {
Auth
. Inmemoryauthentication ()
. Withuser ("admin"). Password ("123456"). Roles ("USER").);
}
}
Open Spring security features with @enablewebsecurity annotations
Inherit the Websecurityconfigureradapter, and rewrite its methods to set some Web security details
The Configure (httpsecurity http) method, through Authorizerequests (), defines which URLs need to be protected and which do not need to be protected. For example, the above code specifies that/and/home does not require any authentication to be accessible, and the other paths must be authenticated.
Formlogin () defines the login page to go to when a user is required to log in.
The Configureglobal (Authenticationmanagerbuilder auth) method creates a user in memory that has the name admin, password 123456, and user role.
Controller:
@Controller Public classHellocontroller {
@RequestMapping ("/")
PublicString Index () {
return"Index";
}
@RequestMapping ("/hello")
PublicString Hello () {
return"Hello";
}
@RequestMapping (Value= "/login", method =requestmethod.get)
PublicString Login () {
return"Login";
}
}
Index.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>Getting Started with Spring security</title>
</Head>
<Body>
<H1>Welcome to Spring security!</H1>
<P>Click<aTh:href= "@{/hello}">Over here</a>Say hello.</P>
</Body>
</HTML>
Hello.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>Hello world!</title>
</Head>
<Body>
<H1Th:inline= "text">Hello [[${#httpServletRequest. remoteuser}]]!</H1>
<formth:action= "@{/logout}"Method= "POST">
<inputtype= "Submit"value= "Logoff"/>
</form>
</Body>
</HTML>
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>
Run:
Open index.html, click here, if not logged into the login page, have logged in to jump to hello.html
http://blog.didispace.com/springbootsecurity/
Spring Boot SpringSecurity5 Authentication