【Spring Boot】011-SpringSecurity(安全)

落日映苍穹つ 2023-10-05 10:10 81阅读 0赞

最新更新:2020年9月22日08:16:43

目录

一、概述

二、环境搭建

1、创建项目

第一步:配置项目相关信息

第二步:勾选web模块,创建即可

2、引入相关坐标

thymeleaf模板引擎:

3、导入静态资源

资源下载地址:

4、设置配置文件application.properties

5、编写RouterController类

6、运行测试

三、用户授权和认证

1、用户授权

第一步:导入security依赖坐标

第二步:创建SecurityConfig类

第三步:运行测试

第四步:设置若没有权限跳转到登陆页面

2、用户认证

第一步:修改Config类:

第二步:测试结果:

第三步:密码加密:

第四步:测试结果:(成功登陆)

第五步:可正常访问相关权限内页面:

四、注销及权限控制

1、注销

第一步:修改Config类

第二步:修改index.html页面

第三步:测试结果

第四步:修改代码,使其跳转到首页

第五步:测试结果

2、权限控制

第一步:导入坐标

第二步:修改index.html页面

第三步:修改spring boot版本为2.0.7,因为2.3.3springboot 2.1.x版本以上不兼容部分标签,如

第四步:运行测试

备注:

五、开启“记住我”功能

修改SecurityConfig类:

六、备注

1、说明

2、注意最佳实践结合:

七、源代码


一、概述

Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理这个领域,一直是 Shiro 的天下。

相对于 Shiro,在 SSM/SSH 中整合 Spring Security 都是比较麻烦的操作,所以,Spring Security 虽然功能比 Shiro 强大,但是使用反而没有 Shiro 多(Shiro 虽然功能没有 Spring Security 多,但是对于大部分项目而言,Shiro 也够用了)。

自从有了 Spring Boot 之后,Spring Boot 对于 Spring Security 提供了 自动化配置方案,可以零配置使用 Spring Security。

因此,一般来说,常见的安全管理技术栈的组合是这样的:

  • SSM + Shiro
  • Spring Boot/Spring Cloud + Spring Security

注意,这只是一个推荐的组合而已,如果单纯从技术上来说,无论怎么组合,都是可以运行的。

二、环境搭建

1、创建项目

第一步:配置项目相关信息

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70

第二步:勾选web模块,创建即可

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 1

2、引入相关坐标

thymeleaf模板引擎:

  1. <!--thymeleaf-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.thymeleaf.extras</groupId>
  8. <artifactId>thymeleaf-extras-java8time</artifactId>
  9. </dependency>

3、导入静态资源

资源下载地址:

链接:https://pan.baidu.com/s/1XGpVIQwN3U9NICEVegC1Mw
提取码:zibo

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 2

4、设置配置文件application.properties

  1. spring.thymeleaf.cache=false

5、编写RouterController类

  1. package com.zibo.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @Controller
  6. public class RouterController {
  7. @RequestMapping({"/","index","index.html"})
  8. public String index(){
  9. return "index";
  10. }
  11. @RequestMapping("/toLogin")
  12. public String toLogin(){
  13. return "views/login";
  14. }
  15. @RequestMapping("/level1/{id}")
  16. public String level1(@PathVariable("id") int id){
  17. return "views/level1/" + id;
  18. }
  19. @RequestMapping("/level2/{id}")
  20. public String level2(@PathVariable("id") int id){
  21. return "views/level2/" + id;
  22. }
  23. @RequestMapping("/level3/{id}")
  24. public String level3(@PathVariable("id") int id){
  25. return "views/level3/" + id;
  26. }
  27. }

6、运行测试

http://localhost:8080/

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 3

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 4

三、用户授权和认证

1、用户授权

第一步:导入security依赖坐标

  1. <!--security-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-security</artifactId>
  5. </dependency>

第二步:创建SecurityConfig类

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  3. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. //AOP:拦截器
  6. @EnableWebSecurity
  7. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  8. @Override
  9. protected void configure(HttpSecurity http) throws Exception {
  10. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  11. //链式编程
  12. http.authorizeRequests()
  13. .antMatchers("/").permitAll()
  14. .antMatchers("/level1/**").hasRole("vip1")
  15. .antMatchers("/level2/**").hasRole("vip2")
  16. .antMatchers("/level3/**").hasRole("vip3");
  17. }
  18. }

第三步:运行测试

首页可以正常访问:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 5

其他页面无法访问:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 6

第四步:设置若没有权限跳转到登陆页面

修改Config类:

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  3. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. //AOP:拦截器
  6. @EnableWebSecurity
  7. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  8. @Override
  9. protected void configure(HttpSecurity http) throws Exception {
  10. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  11. //链式编程
  12. http.authorizeRequests()
  13. .antMatchers("/").permitAll()
  14. .antMatchers("/level1/**").hasRole("vip1")
  15. .antMatchers("/level2/**").hasRole("vip2")
  16. .antMatchers("/level3/**").hasRole("vip3");
  17. //没有权限,默认调到登陆页面
  18. http.formLogin();
  19. }
  20. }

测试结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 7

源代码注释:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 8

2、用户认证

第一步:修改Config类:

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. //AOP:拦截器
  7. @EnableWebSecurity
  8. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  9. //授权
  10. @Override
  11. protected void configure(HttpSecurity http) throws Exception {
  12. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  13. //链式编程
  14. http.authorizeRequests()
  15. .antMatchers("/").permitAll()
  16. .antMatchers("/level1/**").hasRole("vip1")
  17. .antMatchers("/level2/**").hasRole("vip2")
  18. .antMatchers("/level3/**").hasRole("vip3");
  19. //没有权限,默认调到登陆页面
  20. http.formLogin();
  21. }
  22. //认证
  23. @Override
  24. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  25. //这些数据正常情况下应该从数据库中读取
  26. auth.inMemoryAuthentication()
  27. .withUser("zibo").password("123").roles("vip2","vip3")
  28. .and()
  29. .withUser("zb").password("123").roles("vip1","vip3");
  30. }
  31. }

第二步:测试结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 9

报错,因为密码未加密!

第三步:密码加密:

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  7. //AOP:拦截器
  8. @EnableWebSecurity
  9. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  10. //授权
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  14. //链式编程
  15. http.authorizeRequests()
  16. .antMatchers("/").permitAll()
  17. .antMatchers("/level1/**").hasRole("vip1")
  18. .antMatchers("/level2/**").hasRole("vip2")
  19. .antMatchers("/level3/**").hasRole("vip3");
  20. //没有权限,默认调到登陆页面
  21. http.formLogin();
  22. }
  23. //认证
  24. //PasswordEncoder 密码编码(加密)
  25. //在Spring Security5.0+中,新增了很多加密方法
  26. @Override
  27. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  28. //这些数据正常情况下应该从数据库中读取
  29. auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
  30. .withUser("zibo").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
  31. .and()
  32. .withUser("zb").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip3");
  33. }
  34. }

第四步:测试结果:(成功登陆)

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 10

第五步:可正常访问相关权限内页面:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 11

四、注销及权限控制

1、注销

第一步:修改Config类

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  7. //AOP:拦截器
  8. @EnableWebSecurity
  9. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  10. //授权
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  14. //链式编程
  15. http.authorizeRequests()
  16. .antMatchers("/").permitAll()
  17. .antMatchers("/level1/**").hasRole("vip1")
  18. .antMatchers("/level2/**").hasRole("vip2")
  19. .antMatchers("/level3/**").hasRole("vip3");
  20. //没有权限,默认调到登陆页面
  21. http.formLogin();
  22. //★★★注销
  23. http.logout();
  24. }
  25. //认证
  26. //PasswordEncoder 密码编码(加密)
  27. //在Spring Security5.0+中,新增了很多加密方法
  28. @Override
  29. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  30. //这些数据正常情况下应该从数据库中读取
  31. auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
  32. .withUser("zibo").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
  33. .and()
  34. .withUser("zb").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip3");
  35. }
  36. }

第二步:修改index.html页面

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  6. <title>首页</title>
  7. <!--semantic-ui-->
  8. <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
  9. <link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
  10. </head>
  11. <body>
  12. <!--主容器-->
  13. <div class="ui container">
  14. <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
  15. <div class="ui secondary menu">
  16. <a class="item" th:href="@{/index}">首页</a>
  17. <!--登录注销-->
  18. <div class="right menu">
  19. <!--未登录-->
  20. <a class="item" th:href="@{/toLogin}">
  21. <i class="address card icon"></i> 登录
  22. </a>
  23. <a class="item" th:href="@{/logout}">
  24. <i class="address card icon"></i> 注销
  25. </a>
  26. <!--已登录
  27. <a th:href="@{/usr/toUserCenter}">
  28. <i class="address card icon"></i> admin
  29. </a>
  30. -->
  31. </div>
  32. </div>
  33. </div>
  34. <div class="ui segment" style="text-align: center">
  35. <h3>Spring Security Study by 秦疆</h3>
  36. </div>
  37. <div>
  38. <br>
  39. <div class="ui three column stackable grid">
  40. <div class="column">
  41. <div class="ui raised segment">
  42. <div class="ui">
  43. <div class="content">
  44. <h5 class="content">Level 1</h5>
  45. <hr>
  46. <div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
  47. <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
  48. <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. <div class="column">
  54. <div class="ui raised segment">
  55. <div class="ui">
  56. <div class="content">
  57. <h5 class="content">Level 2</h5>
  58. <hr>
  59. <div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
  60. <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
  61. <div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <div class="column">
  67. <div class="ui raised segment">
  68. <div class="ui">
  69. <div class="content">
  70. <h5 class="content">Level 3</h5>
  71. <hr>
  72. <div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
  73. <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
  74. <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
  83. <script th:src="@{/qinjiang/js/semantic.min.js}"></script>
  84. </body>
  85. </html>

第三步:测试结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 12

第四步:修改代码,使其跳转到首页

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  7. //AOP:拦截器
  8. @EnableWebSecurity
  9. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  10. //授权
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  14. //链式编程
  15. http.authorizeRequests()
  16. .antMatchers("/").permitAll()
  17. .antMatchers("/level1/**").hasRole("vip1")
  18. .antMatchers("/level2/**").hasRole("vip2")
  19. .antMatchers("/level3/**").hasRole("vip3");
  20. //没有权限,默认调到登陆页面
  21. http.formLogin();
  22. //★★★注销
  23. http.logout().logoutSuccessUrl("/");
  24. }
  25. //认证
  26. //PasswordEncoder 密码编码(加密)
  27. //在Spring Security5.0+中,新增了很多加密方法
  28. @Override
  29. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  30. //这些数据正常情况下应该从数据库中读取
  31. auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
  32. .withUser("zibo").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
  33. .and()
  34. .withUser("zb").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip3");
  35. }
  36. }

第五步:测试结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 13

2、权限控制

第一步:导入坐标

  1. <!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
  2. <dependency>
  3. <groupId>org.thymeleaf.extras</groupId>
  4. <artifactId>thymeleaf-extras-springsecurity4</artifactId>
  5. <version>3.0.4.RELEASE</version>
  6. </dependency>

第二步:修改index.html页面

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org"
  3. xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  7. <title>首页</title>
  8. <!--semantic-ui-->
  9. <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
  10. <link th:href="@{/qinjiang/css/qinstyle.css}" rel="stylesheet">
  11. </head>
  12. <body>
  13. <!--主容器-->
  14. <div class="ui container">
  15. <div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
  16. <div class="ui secondary menu">
  17. <a class="item" th:href="@{/index}">首页</a>
  18. <!--登录注销-->
  19. <div class="right menu">
  20. <!--未登录:显示登录按钮-->
  21. <div sec:authorize="!isAuthenticated()">
  22. <a class="item" th:href="@{/toLogin}">
  23. <i class="address card icon"></i> 登录
  24. </a>
  25. </div>
  26. <!--已登录:显示用户名和注销按钮★★★-->
  27. <div sec:authorize="isAuthenticated()">
  28. <a class="item">
  29. 用户名:<span sec:authentication="name"></span>
  30. 角色:<span sec:authentication="principal.getAuthorities()"></span>
  31. </a>
  32. <a class="item" th:href="@{/logout}">
  33. <i class="address card icon"></i> 注销
  34. </a>
  35. </div>
  36. <!--已登录:显示用户名和注销按钮-->
  37. <!-- <a th:href="@{/usr/toUserCenter}">-->
  38. <!-- <i class="address card icon"></i> admin-->
  39. <!-- </a>-->
  40. </div>
  41. </div>
  42. </div>
  43. <div class="ui segment" style="text-align: center">
  44. <h3>Spring Security Study by 秦疆</h3>
  45. </div>
  46. <div>
  47. <br>
  48. <div class="ui three column stackable grid">
  49. <div class="column">
  50. <div class="ui raised segment">
  51. <div class="ui">
  52. <div class="content">
  53. <h5 class="content">Level 1</h5>
  54. <hr>
  55. <div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
  56. <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
  57. <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
  58. </div>
  59. </div>
  60. </div>
  61. </div>
  62. <div class="column">
  63. <div class="ui raised segment">
  64. <div class="ui">
  65. <div class="content">
  66. <h5 class="content">Level 2</h5>
  67. <hr>
  68. <div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
  69. <div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
  70. <div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. <div class="column">
  76. <div class="ui raised segment">
  77. <div class="ui">
  78. <div class="content">
  79. <h5 class="content">Level 3</h5>
  80. <hr>
  81. <div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
  82. <div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
  83. <div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </div>
  89. </div>
  90. </div>
  91. <script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
  92. <script th:src="@{/qinjiang/js/semantic.min.js}"></script>
  93. </body>
  94. </html>

第三步:修改spring boot版本为2.0.7,因为2.3.3springboot 2.1.x版本以上不兼容部分标签,如

  1. sec:authorize="isAuthenticated()" sec:authorize="isAnonymous()"

第四步:运行测试

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5Njg5MzQz_size_16_color_FFFFFF_t_70 14

备注:

版本兼容问题是一大坨问题,修改版本之后以前的写法可能呈现的结果又不同,具体使用的时候需要细细研究;

五、开启“记住我”功能

修改SecurityConfig类:

  1. package com.zibo.config;
  2. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  7. //AOP:拦截器
  8. @EnableWebSecurity
  9. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  10. //授权
  11. @Override
  12. protected void configure(HttpSecurity http) throws Exception {
  13. //首页所有人可以访问,里面的功能页只能有权限的人才能访问
  14. //链式编程
  15. http.authorizeRequests()
  16. .antMatchers("/").permitAll()
  17. .antMatchers("/level1/**").hasRole("vip1")
  18. .antMatchers("/level2/**").hasRole("vip2")
  19. .antMatchers("/level3/**").hasRole("vip3");
  20. //没有权限,默认调到登陆页面
  21. http.formLogin();
  22. //注销
  23. http.logout().logoutSuccessUrl("/");
  24. //★★★记住我
  25. http.rememberMe();
  26. }
  27. //认证
  28. //PasswordEncoder 密码编码(加密)
  29. //在Spring Security5.0+中,新增了很多加密方法
  30. @Override
  31. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  32. //这些数据正常情况下应该从数据库中读取
  33. auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
  34. .withUser("zibo").password(new BCryptPasswordEncoder().encode("123")).roles("vip2","vip3")
  35. .and()
  36. .withUser("zb").password(new BCryptPasswordEncoder().encode("123")).roles("vip1","vip3");
  37. }
  38. }

六、备注

1、说明

Spring Security目前所学习内容,尚不够用,用时应根据需要细细研究;

2、注意最佳实践结合:

  • SSM + Shiro
  • Spring Boot/Spring Cloud + Spring Security

七、源代码

链接:https://pan.baidu.com/s/1PHm0l-Oop9KuNql6YEeQRg
提取码:zibo

发表评论

表情:
评论列表 (有 0 条评论,81人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringSecurity安全框架

    一、什么是认证和授权 1.1、什么是认证 > 进入移动互联网时代,大家每天都在刷手机,常用的软件有微信、支付宝、头条,抖音等,下边拿微信来举例子说明认证相关的基