spring-security - 如何在 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
解决方案
您可以item.entitlements
使用 Thymeleaf 实用程序方法将其转换为逗号分隔的字符串${#strings.listJoin(item.entitlements,',')}
,然后在hasAnyAuthority
推荐阅读
- sql - 如何在 postgresql 中对 json 类型使用唯一约束
- python - 遍历多个目录的文件以提取数据
- spring - JUnit/Spring/MongoDB:单元测试因空值而失败
- php - 如何在 laravel 中设置两个 id 的关系?
- reactjs - 如何以 react 作为前端,flask 作为后端来启动一个项目?
- python - 将 Pandas Dataframe 转换为嵌套字典
- jekyll - 在 netlify cms 上添加铁饼评论系统和自定义主题
- c# - 使用“is”关键字时已定义局部变量 - 与语言参考不符
- git - 更改用户后无法推拉
- ios - 如何在 Swift(iOS) 中按月对 UITableView 数据进行分组?