Thymeleaf-extras-Spring Security 权限控制

x33g5p2x  于2021-12-24 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(519)

Thymeleaf-extras-Spring Security 概述

Thymeleaf-extras-Spring Security 权限控制

Thymeleaf-extras-Spring Security 概述

1、spring-security 是 spring 提供身份验证和授权的安全框架,用于后台编码,对于前端页面,Thymeleaf 模板引擎对它提供了良好的支持。

2、Spring boot 内置 thymeleaf作为模板引擎,启动器为 spring-boot-starter-thymeleaf,但这个组件只是提供了 Thymeleaf 的核心功能,并没有 spring-security 安全组件的标签。

3、thymeleaf 官网 根据功能不同,提供了不同的组件支持,其中支持 spring-security 的是 **thymeleaf-extras-springsecurity **

ModuleRepository
Core libraryhttps://github.com/thymeleaf/thymeleaf
Spring integrationhttps://github.com/thymeleaf/thymeleaf-spring
Documentationhttps://github.com/thymeleaf/thymeleaf-docs
Testing libraryhttps://github.com/thymeleaf/thymeleaf-testing
Test suiteshttps://github.com/thymeleaf/thymeleaf-tests
Benchmarkshttps://github.com/thymeleaf/thymeleaf-benchmarks
Spring Security integrationhttps://github.com/thymeleaf/thymeleaf-extras-springsecurity
Java 8 Time API compatibilityhttps://github.com/thymeleaf/thymeleaf-extras-java8time
Tiles 2 integrationhttps://github.com/thymeleaf/thymeleaf-extras-tiles2
IE Conditional Comments supporthttps://github.com/thymeleaf/thymeleaf-extras-conditionalcomments

4、thymeleaf-extras-springsecurity 是 Thymeleaf 的附加模块,不属于 Thymeleaf  核心的一部分,同样遵循它自己的版本化模式),但得到了 Thymeleaf  团队的完全支持。Thymeleaf 根据 Spring Security 的不同版本提供了自己对应的版本:

thymeleaf-extras-springsecurity3 for integration with Spring Security 3.x
thymeleaf-extras-springsecurity4 for integration with Spring Security 4.x
thymeleaf-extras-springsecurity5 for integration with Spring Security 5.x

5、thymeleaf-extras-springsecurity 的 Maven 依赖 groupId 与 artifactId 写法如下:

| groupId | org.thymeleaf.extras |
| artifactId | Spring Security 3 integration package: thymeleaf-extras-springsecurity3<br> Spring Security 4 integration package: thymeleaf-extras-springsecurity4<br> Spring Security 5 integration package: thymeleaf-extras-springsecurity5 |

6、thymeleaf-extras-springsecurity 方言所有版本的名称空间是 http://www.thymeleaf.org/extras/spring-security),使用不正确的命名空间虽然不会影响模板的处理,但是,当涉及到模板中的建议/自动补全等问题时,它可能会影响您的 IDE:

<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

Thymeleaf-extras-Spring Security 权限控制

1、本文环境:Spring boot 2.1.3 + Spring Security5.1.4 + Thymeleaf-extras-springsecurity5 :
关于 Spring Security 的内容,可以参考《Spring Security 安全框架概述 与 快速入门

关于 Thymeleaf 的内容,可以参考《Thymeleaf 模板引擎简介 与 Spring Boot 整合入门

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/>
  10. <!-- lookup parent from repository -->
  11. </parent>
  12. <groupId>www.wmx.com</groupId>
  13. <artifactId>spring-security-app</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>spring-security-app</name>
  16. <description>Demo project for Spring Boot</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <!-- Spring security 安全组件-->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-security</artifactId>
  25. </dependency>
  26. <!-- Thymeleaf 模板引擎-->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  30. </dependency>
  31. <!-- Thymeleaf 整合 Spring Security 组件-->
  32. <dependency>
  33. <groupId>org.thymeleaf.extras</groupId>
  34. <artifactId>thymeleaf-extras-springsecurity5</artifactId>
  35. </dependency>
  36. <!-- web组件-->
  37. <dependency>
  38. <groupId>org.springframework.boot</groupId>
  39. <artifactId>spring-boot-starter-web</artifactId>
  40. </dependency>
  41. <!-- Spring boot 测试模块-->
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. <!-- spring security 测试模块-->
  48. <dependency>
  49. <groupId>org.springframework.security</groupId>
  50. <artifactId>spring-security-test</artifactId>
  51. <scope>test</scope>
  52. </dependency>
  53. <!--添加热部署-->
  54. <dependency>
  55. <groupId>org.springframework.boot</groupId>
  56. <artifactId>spring-boot-devtools</artifactId>
  57. <optional>true</optional>
  58. <scope>true</scope>
  59. </dependency>
  60. </dependencies>
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. <configuration>
  67. <!--fork:如果没有该项配置,整个devtools不会起作用-->
  68. <fork>true</fork>
  69. </configuration>
  70. </plugin>
  71. </plugins>
  72. </build>
  73. </project>

2、后台代码和平常没什么区别,不同点是前端 html 页面中使用 thymeleaf 标签即可:

  1. <!DOCTYPE html>
  2. <!--<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">-->
  3. <html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
  4. <head lang="en">
  5. <meta charset="UTF-8">
  6. <title>蚩尤后裔</title>
  7. </head>
  8. <body>
  9. <h2>欢迎来到大熊山</h2>
  10. <div sec:authorize="!isAuthenticated()">
  11. <span style="color: red">温馨提示:使用前请先登录<a th:href="@{/login}">登录</a></span>
  12. </div>
  13. <div sec:authorize="isAuthenticated()">
  14. 当前登录用户:<span sec:authentication="name">zhangWuJi</span>&nbsp;&nbsp;当前角色:<span
  15. sec:authentication="principal.authorities"></span><br>
  16. <a href="#" th:href="@{/logout}">注销</a><br><br>
  17. </div>
  18. <fieldset>
  19. <legend>学生管理</legend>
  20. <div sec:authorize="hasRole('topLevel')">
  21. <a th:href="@{/user/addUser}">添加学生</a><br><br>
  22. </div>
  23. <div sec:authorize="hasAnyRole('topLevel','senior')">
  24. <a th:href="@{/user/deleteUser/101}">删除学生</a><br><br>
  25. </div>
  26. <div sec:authorize="hasAnyRole('topLevel','senior','middleRank')">
  27. <a th:href="@{/user/updateUser}">修改学生</a><br><br>
  28. </div>
  29. <a th:href="@{/user/findAllUsers}">查询学生</a><br><br>
  30. </fieldset>
  31. </body>
  32. </html>
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"<html> 中加入命名空间  xmlns:sec,这样 IDE 在编写标签的时候就会有提示信息。不加也不影响运行。
sec:authorize="!isAuthenticated()"判断当前用户是否已经进行用户认证,已经认证通过时返回 true,继续执行里面的内容,否则不执行。"!"表示取反。
 sec:authentication="name"获取当前认证用户的用户名,也就是后台 configure(AuthenticationManagerBuilder auth) 方法中 withUser("xxx") 设置的用户名称。值会作为标签提的内容。
sec:authentication="principal.authorities"获取当前认证用户的角色,因为一个用户可以有多个角色,所以值是一个数组。
sec:authorize="hasRole('topLevel')"判断当前用户如果有 topLevel 角色则返回 true,同时指向里面的内容,否则不执行
sec:authorize="hasAnyRole('topLevel','senior')"判断当前用户是否有 'topLevel' 角色,或者 'senior'角色,有则继续指向内部的内容,否则不执行。(完全对应后台 http.authorizeRequests() 设置的角色)

3、下面以用户 yangGuo 可以访问 updateUser、findAllUsers 方法 为例进行演示:

查询学生针对所有用户开放,所以未登录时也可以访问。

未登录时无法看到用于权限的内容,登录后也只能看到自己角色权限之内的内容。

更多关于 Thymeleaf-Extras-SpringSecurity 插件的内容参考官网:https://github.com/thymeleaf/thymeleaf-extras-springsecurity

相关文章