加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring – 使用@PreAuthorize Annotation阻止没有异常的方法调

发布时间:2020-12-15 01:36:48 所属栏目:大数据 来源:网络整理
导读:我们正在使用Spring Security 3.我们有一个PermissionEvaluator的自定义实现,它具有这种复杂的算法,可以在应用程序的方法级别授予或拒绝访问.为此,我们将@PreAuthorize注释添加到我们想要保护的方法(显然).一切都很好.然而,我们正在寻找的行为是,如果拒绝has

我们正在使用Spring Security 3.我们有一个PermissionEvaluator的自定义实现,它具有这种复杂的算法,可以在应用程序的方法级别授予或拒绝访问.为此,我们将@PreAuthorize注释添加到我们想要保护的方法(显然).一切都很好.然而,我们正在寻找的行为是,如果拒绝hasPermission调用,则只需要跳过受保护的方法调用,而不是每次发生时都会收到403错误.

任何想法如何预防?

你可以在这里找到对问题的不同解释; AccessDeniedException handling during methodSecurityInterception

最佳答案
解决方案是使用自定义MethodSecurityInterceptor,它调用AccessDecisionManager(隐式地,调用super的方法)并决定是否继续进行方法调用.

package com.myapp;

public class MyMethodSecurityInterceptor extends MethodSecurityInterceptor {

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        Object result = null;
        try {
             InterceptorStatusToken token = super.beforeInvocation(mi);             
        } catch (AccessDeniedException e) {
            // access denied - do not invoke the method and  return null
            return null;
        }

        // access granted - proceed with the method invocation
        try {
            result = mi.proceed();
        } finally {
            result = super.afterInvocation(token,result);
        }

        return result;        
        }
}

设置应用程序上下文有点棘手:因为你不能使用< sec:global-mathod-security>在这种情况下,需要定义一个显式的AOP配置(并默认创建原始标签所做的大部分相应的bean结构):

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读