spring-security ACL如何授予权限

发布于 2024-12-05 17:50:16 字数 234 浏览 0 评论 0原文

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

ACL 是否只向用户/角色授予单个对象的权限,还是为整个类型授予权限?据我了解,域对象意味着类型,但示例和教程似乎将权限分配给特定对象。我只是感到困惑还是我可以两者都做?如果没有的话,其他的我该怎么办?

谢谢!

I'm currently integrating springs-security into our new web application stack. We will need to be able to grant permissions for a user or role to access a specific object or all objects of a certain type. However that's one thing I didn't really get when working through documentations and examples:

Does an ACL only grant permissions to a user/role for a single object or does it do that for the entire type? As I understand it, domain object means the type but the examples and tutorials seem like they assign permissions to specific objects. Am I just confused or can I do both? If not, how do I do the other?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏蝶舞 2024-12-12 17:50:17

有了 spring-security,你就可以做到这两点。这是可能的,因为 spring-security 支持所谓的权限规则 - 在 spring-security 术语中,他们称之为权限评估器。权限规则包含 ACL,而且您还可以在对象实例处于某种状态时保护它们的安全......等等。

这是它的工作原理:

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

    公共类 SomePermissionsEvaluator 实现 PermissionEvaluator {
        @覆盖
        public boolean hasPermission(身份验证authentication,对象targetDomainObject,对象权限){
            if (permission.equals("do_something") && 
            /*认证机构的角色是A*/) {
                返回真
            } else if (permission.equals("do_something_else") && 
            /*认证机构的角色为B*/) {
                return /*如果 targetDomainObject 满足一定条件则返回 true*/;
            }
    
            返回假;
        }
    
        @覆盖
        公共布尔hasPermission(身份验证身份验证,
            可序列化的 targetId、字符串 targetType、对象权限) {
        抛出新的 UnsupportedOperationException();
        }
    }
    
  2. 现在你有了安全规则,你需要通过注释来应用它:

    @PreAuthorize("hasRole('SOME_ROLE_OR_RIGHT') 和" +
    “ hasPermission(#someDomainObject, 'do_something')")
    公共无效updateSomeDomainObject(SomeDomainObject someDomainObject){
        // 在更新对象之前 spring-security 会检查安全规则
    }
    
  3. 为了使其正常工作,应在 applicationContext.xml 中启用安全注释>:

    
        <表达式处理程序ref =“表达式处理程序”/> >
    
    
    
        
            
        
    
    

With spring-security you can do both. It's possible because spring-security supports the so called permission rules - within the spring-security terminology they call it permission evaluators. Permission rules encompass ACL, but also you can secure instances of objects when they're in a certain state...etc.

This is how it works:

  1. You need to extend the PermissionEvaluator - this allows you to have super custom logic for determining access rights - you can check the type of the object or check for a particular id, or check if the user invoking the method is the user that created the object, etc.:

    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. Now that you have a security rule, you need to apply it through annotations:

    @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. In order for this to work the security annotations should be enabled in the 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>
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文