【发布时间】:2020-08-05 08:42:22
【问题描述】:
我正在合作
- Spring Boot 2.2.5
- 百里香 3.0.11
- Thymeleaf Spring Security 5、3.0.4 发布
我在 pom.xml 中使用以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
版本被识别并且相应的 jars 包含在我的项目中。我还在模板中添加了 Thymeleaf 安全模块的额外命名空间:
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
我已经使用 Hibernate 和 MySQL 以及登录/注销实现了基于角色的身份验证和授权,据我了解,这很好。
Thymeleaf Security Dialect 的 sec:authentication 标签工作正常,并且还显示了正确的角色。在调用方法sec:authorize="isAuthenticated()" 时,sec:authorize 标记似乎也能正常工作。
但是,在使用sec:authorize="hasRole('...')" 或th:if="${#authorization.expression('hasRole(''...'')')}" 评估模板中用户的角色时,我遇到了困难。我似乎无法评估角色尽管使用sec:authentication="principal.authorities"正确显示了这些角色。
我在测试页面上总结了我的方法,显示以下结果:
任何线索我的错误或误解隐藏在哪里?非常感谢您提前的支持。
【问题讨论】:
标签: java spring-boot spring-security thymeleaf