使用@PreAuthorize Annotation防止没有异常的方法调用

Che*_*ech 6 spring spring-security

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

任何想法如何预防?


你可以在这里找到对问题的不同解释; methodSecurityInterception期间的AccessDeniedException处理

Bor*_*ner 4

解决方案是使用 custom 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;        
        }
}
Run Code Online (Sandbox Code Playgroud)

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

<aop:config>
    <!-- Intercept all relevant methods -->
    <aop:pointcut id="myMethods"
                  expression='execution(* com.myapp.myService+.*(..))'/>
    <aop:advisor advice-ref="mySecurityInterceptor" pointcut-ref="myMethods"/>
</aop:config>

<!-- Configure custom security interceptor -->
<bean id="mySecurityInterceptor"
      class="com.myapp.MyMethodSecurityInterceptor">
    <property name="securityMetadataSource">
        <bean class="org.springframework.security.access.prepost.PrePostAnnotationSecurityMetadataSource">
            <constructor-arg>
                <bean class="org.springframework.security.access.expression.method.ExpressionBasedAnnotationAttributeFactory">
                    <constructor-arg>
                        <bean class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler"/>
                    </constructor-arg>
                </bean>
            </constructor-arg>
        </bean>
    </property>
    <property name="validateConfigAttributes" value="false"/>
    <property name="accessDecisionManager" ref="accessDecisionManager"/>
    <property name="authenticationManager" ref="authenticationManager"/>
</bean>

<!-- Configure AccessDecisionManager -->
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
    <property name="decisionVoters">
        <list>
            <bean class="org.springframework.security.access.prepost.PreInvocationAuthorizationAdviceVoter">
                <constructor-arg>
                    <bean class="org.springframework.security.access.expression.method.ExpressionBasedPreInvocationAdvice"/>
                </constructor-arg>
            </bean>
        </list>
    </property>
</bean>

<!-- Configure AuthenticationManager as you wish -->
<!-- ........................................... -->
Run Code Online (Sandbox Code Playgroud)