【问题标题】:spring-security how ACL grants permissionsspring-security ACL如何授予权限
【发布时间】:2011-11-20 20:45:40
【问题描述】:

我目前正在将 springs-security 集成到我们的新 Web 应用程序堆栈中。我们需要能够授予用户或角色访问特定对象或特定类型的所有对象的权限。然而,这是我在阅读文档和示例时并没有真正得到的一件事:

ACL 是仅授予用户/角色对单个对象的权限,还是对整个类型授予权限?据我了解,domain object 表示类型,但示例和教程似乎将权限分配给特定对象。我只是困惑还是我可以两者兼而有之?如果没有,我该怎么做?

谢谢!

【问题讨论】:

    标签: permissions spring-security acl


    【解决方案1】:

    使用 spring-security 你可以做到这两点。这是可能的,因为 spring-security 支持所谓的权限规则 - 在 spring-security 术语中,他们称之为 permission evaluators。权限规则包括 ACL,但您也可以在对象处于特定状态时保护它们的实例……等等。

    这就是它的工作原理:

    1. 您需要扩展 PermissionEvaluator - 这允许您拥有超级自定义逻辑来确定访问权限 - 您可以检查对象的类型或检查特定的 id,或者检查调用该方法的用户是否是创建对象等:

      public class SomePermissionsEvaluator implements PermissionEvaluator {
          @Override
          public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
              if (permission.equals("do_something") && 
              /*authentication authorities has the role A*/) {
                  return true
              } else if (permission.equals("do_something_else") && 
              /*authentication authorities has the role B*/) {
                  return /*true if targetDomainObject satisfies certain condition*/;
              }
      
              return false;
          }
      
          @Override
          public boolean hasPermission(Authentication authentication,
              Serializable targetId, String targetType, Object permission) {
          throw new UnsupportedOperationException();
          }
      }
      
    2. 现在你有了一个安全规则,你需要通过注解来应用它:

      @PreAuthorize("hasRole('SOME_ROLE_OR_RIGHT') and" +
      " hasPermission(#someDomainObject, 'do_something')")
      public void updateSomeDomainObject(SomeDomainObject someDomainObject) {
          // before updating the object spring-security will check the security rules
      }
      
    3. 为了使其正常工作,应在 applicationContext.xml 中启用安全注释:

      <global-method-security secured-annotations="enabled" pre-post-annotations="enabled">
          <expression-handler ref="expressionHandler"/>
      </global-method-security>
      
      <beans:bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
          <beans:property name="permissionEvaluator">
              <beans:bean id="permissionEvaluator" class="com.npacemo.permissions.SomePermissionsEvaluator"/>
          </beans:property>
      </beans:bean>
      

    【讨论】:

    • 啊,感谢您的快速回答,没想到在他们使用某种ObjectIdentityRetrievalStrategyAclPermissionEvaluator 实现中实现PermissionEvaluator 并不容易...跨度>
    • 列表和代码都可以高亮,只需要多加四个空格。我编辑了这篇文章作为示例,请参阅源代码。 :)
    猜你喜欢
    • 2011-09-02
    • 2014-02-18
    • 1970-01-01
    • 2011-04-20
    • 2013-10-14
    • 1970-01-01
    • 2013-04-17
    • 2014-05-11
    相关资源
    最近更新 更多