【发布时间】:2021-08-11 12:00:38
【问题描述】:
有多个组织单位,每个单位都有一个自定义角色。在页面上,授权后,您需要显示一个仅用于该角色(部门)的按钮。此代码有效:
<div>
<a th:sec:authorize="hasRole('ROLE_DEP-1')" href="/userPage/department1" type="button">DEP-1</a>
<a th:sec:authorize="hasRole('ROLE_DEP-2')" href="/userPage/department2" type="button">DEP-2</a>
<a th:sec:authorize="hasRole('ROLE_DEP-3')" href="/userPage/department3" type="button">DEP-3</a>
<a th:sec:authorize="hasRole('ROLE_DEP-4')" href="/userPage/department4" type="button">DEP-4</a>
</div>
但我认为手动编写所有细分是不正确的,因为将来可能会添加更多单元。我决定通过 th: 每个 Thymeleaf 循环来完成:
<div th:each="d : ${departments}">
<a th:sec:authorize="hasRole('+${d.role.name}+')" href="/userPage/department" type="button" th:text="${d.shortName}"></a>
</div>
但不幸的是,这不起作用,按钮不显示。角色名称显示正确。部门表和角色表之间的关系是 1:1。我不明白为什么它不起作用。 在这个函数中可能需要有一个特殊的语法:
"hasRole('+${d.role.name}+')"
【问题讨论】:
标签: html spring-security authorization thymeleaf