欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringSecurity身份验证基础入门

程序员文章站 2022-10-18 12:22:57
对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过Aop、拦截器实现,也可以通过框架实现(如:Apache Shiro、Spring Security)。 pom.xml添加依赖 创建SpringSecurity配置类 通过@EnableWebSecurity注解开启 ......

对于没有访问权限的用户需要转到登录表单页面。要实现访问控制的方法多种多样,可以通过aop、拦截器实现,也可以通过框架实现(如:apache shiro、spring security)。

pom.xml添加依赖

 1 <dependency>
 2             <groupid>org.springframework.boot</groupid>
 3             <artifactid>spring-boot-starter-web</artifactid>
 4         </dependency>
 5  
 6         <dependency>
 7             <groupid>org.springframework.boot</groupid>
 8             <artifactid>spring-boot-starter-thymeleaf</artifactid>
 9         </dependency>
10         <dependency>
11             <groupid>org.springframework.boot</groupid>
12             <artifactid>spring-boot-starter-security</artifactid>
13         </dependency>

 

创建springsecurity配置类

 1 import org.springframework.beans.factory.annotation.autowired;
 2 import org.springframework.context.annotation.configuration;
 3 import org.springframework.security.config.annotation.authentication.builders.authenticationmanagerbuilder;
 4 import org.springframework.security.config.annotation.web.builders.httpsecurity;
 5 import org.springframework.security.config.annotation.web.configuration.enablewebsecurity;
 6 import org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter;
 7  
 8 @configuration
 9 @enablewebsecurity
10 public class websecurityconfig extends websecurityconfigureradapter {
11  
12     @override
13     protected void configure(httpsecurity http) throws exception {
14         http
15             .authorizerequests()
16                 .antmatchers("/", "/home").permitall()
17                 .anyrequest().authenticated()
18                 .and()
19             .formlogin()
20                 .loginpage("/login")
21                 .permitall()
22                 .and()
23             .logout()
24                 .permitall();
25     }
26  
27     @autowired
28     public void configureglobal(authenticationmanagerbuilder auth) throws exception {
29         //inmemoryauthentication 从内存中获取
30         auth
31                 .inmemoryauthentication()
32                 .passwordencoder(new bcryptpasswordencoder())
33                 .withuser("admin")
34                 .password(new bcryptpasswordencoder()
35                         .encode("123456")).roles("user");
36     }
37 }

通过@enablewebsecurity注解开启spring security的功能
继承websecurityconfigureradapter,并重写它的方法来设置一些web安全的细节
configure(httpsecurity http)方法,通过authorizerequests()定义哪些url需要被保护、哪些不需要被保护。例如以上代码指定了/和/home不需要任何认证就可以访问,其他的路径都必须通过身份验证。
通过formlogin()定义当需要用户登录时候,转到的登录页面。
configureglobal(authenticationmanagerbuilder auth)方法,在内存中创建了一个用户,该用户的名称为admin,密码为123456,用户角色为user。

 

控制器:

 1 @controller
 2 public class hellocontroller {
 3  
 4     @requestmapping("/")
 5     public string index() {
 6         return "index";
 7     }
 8  
 9     @requestmapping("/hello")
10     public string hello() {
11         return "hello";
12     }
13  
14     @requestmapping(value = "/login", method = requestmethod.get)
15     public string login() {
16         return "login";
17     }
18  
19 }

 

index.html

 1 <!doctype html>
 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
 3       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
 4 <head>
 5     <title>spring security入门</title>
 6 </head>
 7 <body>
 8 <h1>欢迎使用spring security!</h1>
 9  
10 <p>点击 <a th:href="@{/hello}">这里</a> 打个招呼吧</p>
11 </body>
12 </html>

 

hello.html

 1 <!doctype html>
 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
 3       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
 4 <head>
 5     <title>hello world!</title>
 6 </head>
 7 <body>
 8 <h1 th:inline="text">hello [[${#httpservletrequest.remoteuser}]]!</h1>
 9 <form th:action="@{/logout}" method="post">
10     <input type="submit" value="注销"/>
11 </form>
12 </body>
13 </html>

 

login.html

 1 <!doctype html>
 2 <html xmlns="http://www.w3.org/1999/xhtml"
 3       xmlns:th="http://www.thymeleaf.org"
 4       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
 5 <head>
 6     <title>spring security example </title>
 7 </head>
 8 <body>
 9 <div th:if="${param.error}">
10     用户名或密码错
11 </div>
12 <div th:if="${param.logout}">
13     您已注销成功
14 </div>
15 <form th:action="@{/login}" method="post">
16     <div><label> 用户名 : <input type="text" name="username"/> </label></div>
17     <div><label> 密 码 : <input type="password" name="password"/> </label></div>
18     <div><input type="submit" value="登录"/></div>
19 </form>
20 </body>
21 </html>

 

运行:

打开index.html,点击这里,如果没有登录进入登录页,已登录跳转到hello.html

转载于: