首页 > 解决方案 > 具有动态权限检查的 Spring 安全性

问题描述

<div xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sec="http://www.w3.org/1999/xhtml">
 <span sec:authorize="hasAnyRole(${p})">
<button>Save</button>
</span>
</div>

我有这个代码来检查访问保存按钮的权限。

model.addAttribute("p", p); 在控制器中添加 p 作为逗号分隔的权限。那么如何检查用户是否具有任何给定的角色。

标签: spring-securitythymeleaf

解决方案


请考虑使用预处理功能。你可以这样做:

<div xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sec="http://www.w3.org/1999/xhtml">
 <span sec:authorize="hasAnyRole(__${p}__)">
<button>Save</button>
</span>
</div>

请注意使用双下划线开始和结束字符,其中 thymeleaf 先评估此表达式。


推荐阅读