我正在嘗試使用一個屬性來指定要在 jsp sec:authorize 標記中使用的組,因為根據部署此應用程式的區域,這些組會有所不同。使用硬編碼的組名,以下作業:
<sec:authorize access="hasAnyRole('GRP_MY_GROUP_DEV')">
我希望我可以做一些類似的事情(這不起作用,它只是表現得好像我不在組中一樣):
<sec:authorize access="hasAnyRole(${allowed.groups.property})">
uj5u.com熱心網友回復:
您可以創建一個@Bean并參考它。
@Component("authz")
public class JspAuthorizeAccess {
@Value("${allowed.groups.property}")
Set<String> allowedGroups;
public boolean hasAnyRole(Authentication authentication) {
var authorities = authentication.getAuthorities();
var userGroups = AuthorityUtils.authorityListToSet(authorities);
return !Collections.disjoint(allowedGroups, userGroups);
}
}
<sec:authorize access="@authz.hasAnyRole(authentication)">
uj5u.com熱心網友回復:
我最終只是自動裝配組屬性并將其添加到我的控制器中的模型中:
@Value("${allowed.groups.property}")
private String adminGroups;
...
model.addAttribute("adminGroups", adminGroups);
然后在jsp中我有:
<sec:authorize access="hasAnyRole('${adminGroups}')">
這滿足了我的需求,我只是想知道是否可以在 jsp 中更干凈地完成它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383027.html
上一篇:限制惰性列中的專案
