原文链接:Thymeleaf - Spring Security integration modules

来源:thymeleaf/thymeleaf-extras-springsecurity自述文件

状态

这是一个Thymeleaf附加模块,不是Thymeleaf核心的一部分(因此它有自己的版本号),但是Thymeleaf团队提供全部支持。

这个仓库包含3个项目:

  • thymeleaf-extras-springsecurity5是与Spring Security 5.x相匹配的集成模块
  • thymeleaf-extras-springsecurity6是与Spring Security 6.x相匹配的集成模块

当前版本:

  • 3.0.4.RELEASE适用于Thymeleaf 3.0(要求Thymeleaf版本高于或等于3.0.10)
  • 2.1.3.RELEASE适用于Thymeleaf 2.1(要求Thymeleaf版本高于或等于2.1.2)

许可

本软件按照Apache License 2.0提供许可。

要求(3.0.x)

  • Thymeleaf版本高于或等于3.0.10
  • Spring框架版本在3.0.x至5.1.x之间
  • Spring Security版本在3.0.x至5.1.x之间
  • 联网环境(Spring Security集成不能离线工作)。还需要Spring MVC和Spring WebFlux才能工作。

Maven信息

  • groupId: org.thymeleaf.extras
  • artifactId:
    • Spring Security 5集成包:thymeleaf-extras-springsecurity5
    • Spring Security 6集成包:thymeleaf-extras-springsecurity6

分发包

分发包(二进制文件、源代码和文档)可以在bintray下载。

特性

模块提供名为org.thymeleaf.extras.springsecurity5.dialect.SpringSecurityDialect或org.thymeleaf.extras.springsecurity6.dialect.SpringSecurityDialect的新方言(具体是哪个方言取决于Spring Security版本),方言默认前缀是sec。这些方言包括:

  • 新的表达式工具对象:

    • #authentication表示Spring Security认证对象(一个实现了org.springframework.security.core.Authentication接口的对象)
    • #authorization:一个表达式工具对象,可以根据表达式、url和访问控制列表检查授权。
  • 新属性:
    • sec:authentication="prop"输出authentication对象的prop属性值,这与JSP标签<sec:authentication />类似。
    • sec:authorize="expr"或sec:authorize-expr="expr"根据用户能否通过expr表达式的授权验证,决定是否渲染该属性所在标签。(sec:authorize="expr" or sec:authorize-expr="expr" renders the element children (tag content) if the authenticated user is authorized to see it according to the specified Spring Security expression.)(译注:上句翻译不贴合本意,是译者根据自己的使用体验写的,下面两句翻译可能也不贴合本意)
    • sec:authorize-url="url"根据用户能否访问特定URL决定是否渲染该属性所在标签。
    • sec:authorize-acl="object :: permissions"根据Spring Source的访问控制列表系统和用户对特定领域对象是否有指定权限来决定是否渲染属性所在标签。

配置

为了在Spring MVC应用中使用thymeleaf-extras-springsecurity[5][6]模块(或者在Spring WebFlux应用中使用thymeleaf-extras-springsecurity6),对于使用Spring和Thymeleaf的应用首先需要使用通常的方式配置应用(TemplateEngine bean、模板解析器等等),然后将Spring Security方言添加到模板引擎中,这样就可以使用sec:*属性和专用表达式工具对象:

<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
...
<property name="additionalDialects">
<set>
<!-- Note the package would change to 'springsecurity[5]' if you are using that version -->
<bean class="org.thymeleaf.extras.springsecurity6.dialect.SpringSecurityDialect"/>
</set>
</property>
...
</bean>

以上就是全部内容了!

注意:如果是在Spring Boot应用中使用Thymeleaf,只需要把相应的Thymeleaf和Spring Security的starter以及thymeleaf-extras-springsecurity[5|6]依赖添加到应用中,方言就自动配置好了。

使用表达式工具对象

#authentication对象使用起来很简单,就像下面这样:

<div th:text="${#authentication.name}">
The value of the "name" property of the authentication object should appear here.
</div>

#authorization也可以以同样的方式使用,通常用在th:if或th:unless标签中:

<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>

#authorization对象是org.thymeleaf.extras.springsecurity[5|6].auth.Authorization类的实例,参阅该类与它的文档来理解它的方法。

使用属性

使用sec:authentication属性基本等同于使用#authentication对象,但是仅能使用该对象的属性:

<div sec:authentication="name">
The value of the "name" property of the authentication object should appear here.
</div>

sec:authorize和sec:authorize-expr属性完全相同。它们和在th:if中使用#authorization.expression(...)效果相同:

<div sec:authorize="hasRole('ROLE_ADMIN')">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>

这些sec:authorize属性中的Spring安全表达式实际上是在springsecurity特定的根对象上计算的Spring EL表达式,根对象包含hasRole(…)、getPrincipal()等方法。(These Spring Security Expressions in sec:authorize attributes are in fact Spring EL expressions evaluated on a SpringSecurity-specific root object containing methods such as hasRole(...), getPrincipal(), etc.)

与普通的Spring EL表达式一样,可以在Thymeleaf中访问一系列对象,这些对象包括上下文变量映射(#vars对象)。事实上,可以用${…}包围访问表达式,如果你觉得这样让你更舒服(译注:意思是不用${…}包围表达式也可以):

<div sec:authorize="${hasRole(#vars.expectedRole)}">
This will only be displayed if authenticated user has a role computed by the controller.
</div>

记住,Spring Security将一个特殊的安全导向的对象设置为表达式根,这就是不能在上述表达式中直接访问expctedRole变量的原因。

另一种检查授权的方式是sec:authorize-url,可以用这个属性检查用户能否访问特定URL:

<div sec:authorize-url="/admin">
This will only be displayed if authenticated user can call the "/admin" URL.
</div>

对于指定特定的HTTP方法,可以这样做:

<div sec:authorize-url="POST /admin">
This will only be displayed if authenticated user can call the "/admin" URL
using the POST HTTP method.
</div>

最后,有一个使用Spring Security的访问控制列表检查授权的属性,它需要一个域对象的规范和我们要求的在它上定义的权限。

<div sec:authorize-acl="${obj} :: '1,3'">
This will only be displayed if authenticated user has permissions "1" and "3"
on domain object referenced by context variable "obj".
</div>

在上面的属性中,域对象和权限规范都是thymeleaf标准表达式。

命名空间

所有版本方言都可用的命名空间是http://www.thymeleaf.org/extras/spring-security。

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

错误的命名空间不会影响模板处理,它可能会影响在IDE中编辑模板时的建议和自动补全。

【翻译】Thymeleaf – Spring Security集成模块的更多相关文章

  1. 【翻译】Spring Security抛弃了WebSecurityConfigurerAdapter

    原文链接:Spring Security without the WebSecurityConfigurerAdapter 作者:ELEFTHERIA STEIN-KOUSATHANA 发表日期:20 ...

  2. 【翻译】Spring Security - 如何解决WebSecurityConfigurerAdapter类已被弃用的问题?

    原文链接:Spring Security - How to Fix WebSecurityConfigurerAdapter Deprecated 原文作者:Nam Ha Minh 原文发表日期:20 ...

  3. SpringBoot集成Spring Security(授权与认证)

    ⒈添加starter依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  4. Spring Security 5.0.x 参考手册 【翻译自官方GIT-2018.06.12】

    源码请移步至:https://github.com/aquariuspj/spring-security/tree/translator/docs/manual/src/docs/asciidoc 版 ...

  5. Spring boot +Spring Security + Thymeleaf 认证失败返回错误信息

    [Please make sure to select the branch corresponding to the version of Thymeleaf you are using] Stat ...

  6. Spring Security(一):官网向导翻译

    原文出自  https://spring.io/guides/topicals/spring-security-architecture Spring Security Architecture   ...

  7. SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置

    官方文档参考,5.1.2 中文参考文档,4.1 中文参考文档,4.1 官方文档中文翻译与源码解读 SpringSecurity 核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) ...

  8. 从源码看Spring Security之采坑笔记(Spring Boot篇)

    一:唠嗑 鼓捣了两天的Spring Security,踩了不少坑.如果你在学Spring Security,恰好又是使用的Spring Boot,那么给我点个赞吧!这篇博客将会让你了解Spring S ...

  9. Spring Security OAuth2 开发指南

    官方原文:http://projects.spring.io/spring-security-oauth/docs/oauth2.html 翻译及修改补充:Alex Liao. 转载请注明来源:htt ...

随机推荐

  1. GNSS模块使用笔记

    目录 目录 GNSS芯片 NMEA0183 协议 指令 GNSS TO MCU MCU TO GNSS GNSS芯片 ATGM336H-5N31(GPS+BDS双模) 原理图 NMEA0183 协议 ...

  2. ABC266.

    D 设 \(f_{t,p}\) 代表在 \(t\) 时间点时人在 \(p\) 点的最大收益,在这一步他可以 \(p\) 增加,不动,\(p\) 减少.于是得出状态转移方程:\(f_{t,p} = \m ...

  3. springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误。

    springMVC配置时,静态资源和jsp文件路径没错但是访问时controller的请求报404错误. 1.场景 如果在web.xml中servlet-mapping的url-pattern设置的是 ...

  4. day36-IO流03

    JavaIO流03 4.常用的类02 4.4节点流和处理流 4.4.1基本介绍 节点流可以从一个特定的数据源读写数据,如FileReader.FileWriter 数据源就是存放数据的地方,比如文件. ...

  5. 2020年12月-第01阶段-前端基础-HTML CSS 项目阶段(三)

    品优购项目(三) 1. 首页制作 1). 楼层区 floor 注意这个floor 一个大盒子 包含, 不要给高度,内容有多少,算多少 2). 家用电器模块 这个模块 简单 不需要写样式 版心居中对齐 ...

  6. 我的Go gRPC之旅、01 初识gRPC,感受gRPC的强大魅力

    微服务架构 微服务是一种开发软件的架构和组织方法,其中软件由通过明确定义的API 进行通信的小型独立服务组成. 这些服务由各个小型独立团队负责. 微服务架构使应用程序更易于扩展和更快地开发,从而加速创 ...

  7. Kratos漫游指南 1 - 概览

    您好,地球人,欢迎来到Kratos漫游指南. 对于刚开始研究Kratos框架的开发者来说,目前的文档有些零散,这与我们的模块化设计有一些关系,不过Don't panic,从这篇文章开始,我将试图打破这 ...

  8. 不给字段创建索引,字段不存放在source中,字段无法聚合查询等

    某个字段不被搜索,也就是说不想为这个字段建立inverted index(反向索引),可以这么做: PUT twitter { "mappings": { "uid&qu ...

  9. 在 CentOS8/RHEL8 中配置 Rsyslog 服务器

    Rsyslog 是一个自由开源的日志记录程序,在 CentOS 8 和 RHEL 8 系统上默认可用.它提供了一种从客户端节点到单个中央服务器的"集中日志"的简单有效的方法.日志集 ...

  10. 内网横向渗透 之 ATT&CK系列一 win7的yxcms

    Yxcms渗透 前言 通过phpMyAdmin慢日志查询Getshell后,蚁剑连接,发现win7下面还有个cms靶场,于是就兴致勃勃的去打了一波. cms渗透 进入页面后,搜刮了页面所有可见信息,发 ...