首页 > 解决方案 > 如何在 Spring Security thymeleaf sec 中使用列表:授权标签

问题描述

我正在使用以下方法循环模型属性:

<li class="nav-item" th:each="item : ${navigation}"
    sec:authorize='hasAnyAuthority("__${item.entitlements}__")'><a
    class="nav-link" th:href="${item.url}" th:text="${item.display}"></a></li>

其中“item.entitlements”是一个包含一个元素的 java List。在每个项目上(此时)。我还验证了该列表中每个项目的权利,并且在 spring 安全上下文中分配的权限是相同的。

我还尝试了以下方法:

${#authorization.expression('hasAnyAuthority(__${item.entitlements}__)')}

这可能吗?如果是这样,我错过了什么?我需要交换我的数据库以使用逗号分隔值的字符串吗?

我正在使用 spring boot 2.1.8.RELEASE 和以下 thymeleaf spring 安全依赖项:

<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>

最终版本为 3.0.4.RELEASE

标签: spring-securitythymeleafspring-el

解决方案


您可以item.entitlements使用 Thymeleaf 实用程序方法将其转换为逗号分隔的字符串${#strings.listJoin(item.entitlements,',')},然后在hasAnyAuthority


推荐阅读