在授权前/授权后获取方法名称

斯里吉斯·奈尔

我正在编写自己的Spring安全性PermissionEvaluator,而我要尝试做的一件事情就是弄清楚它所保护的方法的名称。

例如,如果没有图片中的方法名称,我将具有以下内容:

postAuthorize("hasPermission(returnObject,'read')")
Event getEvent(int evendId) {
...
}

public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
    if(targetDomainObject instanceof Event) {

        return hasPermission(authentication, targetDomainObject, permission);
    }
    return targetDomainObject == null;
}

但是我还需要方法名称“ getEvent”可用于hasPermission。我可以通过在hasPermission调用中手动传递它来完成此操作,如下所示:

@PostAuthorize("hasPermission(new com.example.AuthZObject(returnObject,'getEvent'),'read')")
Event getEvent(int eventId);

但是有没有更自动化的方法来做到这一点?

罗伯·温奇

@Ralph在这里非常正确的想法。第一步是创建MethodSecurityExpressionOperations的自定义实现,该实现可以在上面存储Method并将其用于评估表达式。例如:

public class MyMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
    private Object filterObject;
    private Object returnObject;
    private Object target;
    private Method method;

    public MyMethodSecurityExpressionRoot(Authentication a) {
        super(a);
    }

    // allow the method to be set    
    public void setMethod(Method m) {
        this.method = m;
    }

    // optionally expose the method to be accessed in expressions    
    public Method getMethod() {
        return method;
    }

    // create a method that will perform the check with 
    // the method name transparently for you    
    public boolean hasMethodPermission(Object target, Object permission) {
        boolean result = false;

        // do your calculations using the method member variable 
        // i.e. method.getName() and the arguments passed in
        // of course you could delegate to another object if you want
        // i.e.
        // return hasPermission(new com.example.AuthZObject(target,method.getName()),permission));
        // or you could do the logic right here

        return result;
    }

    // implement the interface and provide setters

    public void setFilterObject(Object filterObject) {
        this.filterObject = filterObject;
    }

    public Object getFilterObject() {
        return filterObject;
    }

    public void setReturnObject(Object returnObject) {
        this.returnObject = returnObject;
    }

    public Object getReturnObject() {
        return returnObject;
    }

    public void setThis(Object target) {
        this.target = target;
    }

    public Object getThis() {
        return target;
    }
}

接下来,扩展DefaultMethodSecurityExpressionHandler并使用自定义表达式根。

public class MySecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler{

    @Override
    protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation){
        MyMethodSecurityExpressionRoot root = new MyMethodSecurityExpressionRoot(authentication);
        root.setThis(invocation.getThis());
        root.setPermissionEvaluator(getPermissionEvaluator());
        root.setTrustResolver(new AuthenticationTrustResolverImpl());
        root.setRoleHierarchy(getRoleHierarchy());
        root.setMethod(invocation.getMethod());
        return root;
    }
}

配置MySecurityExpressionHandler之后,您应该可以使用以下内容:

@PostAuthorize("hasMethodPermission(returnObject,'read')")
Event getEvent(int eventId);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

登录-获取方法名称

来自分类Dev

授权后从URL获取网页文本

来自分类Dev

codeigniter从父方法获取方法名称

来自分类Dev

junit,获取失败的方法名称

来自分类Dev

获取Web服务的方法名称

来自分类Dev

如何获取模型的方法名称数组?

来自分类Dev

获取当前方法名称

来自分类Dev

获取引发异常的方法名称

来自分类Dev

smalltalk反射-如何获取方法名称?

来自分类Dev

401(未经授权)获取方法错误

来自分类Dev

授权后访问 ForgotPasswordController

来自分类Dev

重定向网址后无法获取授权代码

来自分类Dev

获取授权令牌

来自分类Dev

Spring bean方法:如何获取方法名称?

来自分类Dev

动态方法名称

来自分类Dev

使用 keycloak 实现授权,在提取 RPT 令牌后如何将资源名称与控制器映射并取消对用户的授权

来自分类Dev

获取通过lambda表达式传递的方法名称?

来自分类Dev

使用nameof在C#中获取setter方法名称

来自分类Dev

从lambda获取方法名称而无需调用

来自分类Dev

Java获取编译时安全的方法名称

来自分类Dev

在Java过滤器中获取方法名称

来自分类Dev

获取所有服务合同方法名称

来自分类Dev

JS-无法获取函数实例的构造方法名称

来自分类Dev

从非托管库的方法名称中获取参数类型?

来自分类Dev

如何获取在传递参数的lambda中使用的方法名称

来自分类Dev

从AOP调用类获取方法名称和参数

来自分类Dev

在Java过滤器中获取方法名称

来自分类Dev

获取所有服务合同方法名称

来自分类Dev

从Google Auth2新版本获得授权后如何获取授权的电子邮件ID