在春天与thyemeleaf寻求帮助。我想在 spring security hasRole(( 函数中传递特定角色作为参数。
<li sec:authorize="hasRole(__${someVariable}__)" th:if="${#authentication}">
我只想将我的角色传递为变量"某个变量"的值,例如"管理员"、"系统管理员"等
仅供参考。我正在使用 Thymeleaf + Spring
我以一种避免尝试破解双下划线的方式解决了相同的基本问题(例如 __${someVariable}__
( 语法。
<th:block th:with="secAuth=${yourNormalModelAccessHere}">
<li class="nav-item" sec:authorize="${#authorization.expression(#vars.secAuth)}">
</th:block>
希望这对某人有所帮助。 :)
我可以通过以下方式解决它:
${#authorization.expression('hasRole(__${someVariable}__)')}
传递一些变量是什么意思?如果您已正确实现 spring 安全性,则可以使用 sec:authorize="hasRole('ROLE_ADMIN')
来评估 hasRole 。等。此外,您不需要包含th:if
因为 sec:authorize 属性会在属性表达式计算为 true 时呈现其内容
请参阅此处的参考资料
我会放弃sec:authorize
,而是这样写:
<li th:if="${#request.isUserInRole(someVariable)}">
(假设Servlet API的最新版本和Thymeleaf模板中使用Spring Expression Language。