spring-security ACL 如何授予权限 [英] spring-security how ACL grants permissions

查看:37
本文介绍了spring-security ACL 如何授予权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

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?

谢谢!

推荐答案

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

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.

这是它的工作原理:

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

  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();
    }
}

  • 既然你有了安全规则,你需要通过注解来应用它:

  • 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
    }
    

  • 为了使其工作,应在 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>
    

  • 这篇关于spring-security ACL 如何授予权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆