【问题标题】:sec:authorize does not work - Spring Boot 2, Thymeleaf 3, Thymeleaf Spring Security 5 integration packagesec:authorize 不起作用 - Spring Boot 2、Thymeleaf 3、Thymeleaf Spring Security 5 集成包
【发布时间】: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


    【解决方案1】:

    经过更多研究,我发现了我的问题。将前缀 ROLE_ 添加到角色名称(在数据源中)后,一切正常。

    【讨论】:

    • 你不知道我因此遭受了多少痛苦。谢谢,你让我免于扯头发。
    • 很高兴它帮助了你@Samツ
    猜你喜欢
    • 2019-10-04
    • 2021-02-23
    • 2014-07-04
    • 2017-07-25
    • 2015-04-20
    • 2018-06-03
    • 2017-05-14
    • 2017-06-09
    • 2020-11-05
    相关资源
    最近更新 更多