【问题标题】:How to use a property for group in Spring sec:authorize tag for hasAnyRole如何在 Spring sec 中为组使用属性:hasAnyRole 的授权标签
【发布时间】:2022-01-17 04:01:09
【问题描述】:

我正在尝试使用一个属性来指定要在 jsp sec:authorize 标记中使用的组,因为这些组将根据部署此应用程序的区域而有所不同。使用硬编码的组名,以下工作:

<sec:authorize access="hasAnyRole('GRP_MY_GROUP_DEV')">

我希望我可以按照这个思路做一些事情(这是行不通的,它只是表现得好像我不在小组中):

<sec:authorize access="hasAnyRole(${allowed.groups.property})">

【问题讨论】:

    标签: spring-mvc jsp spring-security


    【解决方案1】:

    您可以创建一个@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)">
    

    【讨论】:

      【解决方案2】:

      我最终只是自动装配了 groups 属性并将其添加到我的控制器中的模型中:

      @Value("${allowed.groups.property}")
      private String adminGroups;
       ...
      model.addAttribute("adminGroups", adminGroups);
      

      然后在我的jsp中:

      <sec:authorize access="hasAnyRole('${adminGroups}')">
      

      这可以满足我的需要,我只是想知道我是否可以在 jsp 中更干净地完成它。

      【讨论】:

        猜你喜欢
        • 2016-09-24
        • 2020-03-31
        • 2014-09-13
        • 2019-08-07
        • 2019-03-22
        • 2016-03-14
        • 2018-06-03
        • 2016-02-26
        • 2013-08-11
        相关资源
        最近更新 更多