Spring Boot SpringSecurity5 Authentication

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.