我们正在使用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结构):
fig>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。