Spring Boot (23) using Redis

Source: Internet
Author: User

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

Related Article

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.