【问题标题】:Aspectj and spring security aspects - order of advice executionAspectj 和 spring 安全方面 - 建议执行顺序
【发布时间】:2014-09-05 14:39:53
【问题描述】:

我正在使用 spring-security 3.2.4.RELEASE、spring-security-aspects 3.2.4.RELEASE、AspectJ maven 插件版本 1.6、Java 7。

我使用 AspectJ 的编织而不是 SpringAOP,因此我的 aspectj maven 插件如下所示:

           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.6</version>
                <executions>
                    <execution>                     
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                    </execution>
                </executions>
                <configuration>
                    <Xlint>ignore</Xlint>
                    <showWeaveInfo>true</showWeaveInfo>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <complianceLevel>${org.aspectj-version}</complianceLevel>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework.security</groupId>
                            <artifactId>spring-security-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
          </plugin>

我的另一个方面是这样的:

package com.mycompany.fw.app.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclarePrecedence;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.security.core.parameters.DefaultSecurityParameterNameDiscoverer;

import com.mycompany.fw.security.Integration;

@Aspect
@DeclarePrecedence("IntegrationAspects*,*")
public class IntegrationAspects {

    ParameterNameDiscoverer parameterNameDiscoverer = new DefaultSecurityParameterNameDiscoverer();

    @Pointcut("(execution(* com.mycompany..*(..))) && @annotation(integrate) ")
    public void integratePointCut(Integration integrate) {

    }

    /**
     * TODO: cache
     * 
     * @param jp
     * @param integrate
     * @throws Throwable
     */
    @Around("integratePointCut(integrate)")
    public Object integrate(final ProceedingJoinPoint pjp, Integration integrate) throws Throwable {
        Object res = pjp.proceed();
        return res;
    }

}

我需要将上述(集成方面)放在任何其他方面(包括 Spring 的安全方面)之前 如您所见,我用@DeclarePrecedence 尝试过(我也在.aj 文件中用declare precedence : IntegrationAspects*,* 尝试过),不幸的是,没有成功。

有人可以指导我如何定义方面的调用顺序吗?

【问题讨论】:

    标签: spring-security aspectj spring-aop aspectj-maven-plugin


    【解决方案1】:

    问题是您在@DeclarePrecedence 中没有使用简单的方面名称IntegrationAspects,而是使用小丑字符*。在这种情况下,您需要使用完全限定的类名或百搭类创建相同的名称。

    不起作用:

    @DeclarePrecedence("IntegrationAspects*, *")
    

    作品:

    @DeclarePrecedence("IntegrationAspects, *")
    
    @DeclarePrecedence("com.mycompany.fw.security.Integration.IntegrationAspects, *")
    
    @DeclarePrecedence("com.mycompany.fw.security.Integration.IntegrationAspects*, *")
    
    @DeclarePrecedence("*..IntegrationAspects*, *")
    

    等等。顺便说一句,在类名中使用大写的包名和复数看起来真的很难看。

    我是 AspectJ 专家,不是 Spring 用户,所以我不能告诉你声明优先级是否也会影响 Spring 提供的方面。它还可能取决于它们是使用原生 AspectJ 还是 Spring-AOP(基于代理的“AOP lite”)实现的。

    【讨论】:

    • 感谢您的回答。我没有使用 Spring-AOP,而是使用纯 AspectJ(尽管其中一些方面是由 Spring 家伙编写的)您是否知道从 *.aj 组合方面的限制@DeclarePrecedence 注释中的文件和 *.java 文件?更具体地说,您可以查看 org.springframework.security.access.intercept.aspectj.aspect.AnnotationSecurityAspect 这是一个 *.aj 文件,我正在尝试添加一个将在 secureMethodExecution 方面之前调用的方面
    • 好吧,我不知道您使用的是本机 AspectJ,因为您也使用 spring-aop 标记了问题,并且无论如何语法都是相同的。我的回答能解决你的问题吗?你没有提到任何关于那的事情。您的细微语法错误使其无法在我自己的测试项目中工作,但我的替代方案确实有效。
    • 我注意到的另一个观察结果是,当我添加自己的方面时,应用了我定义的顺序,但是当我将 AnnotationSecurityAspect.aj 添加到订单中时,没有任何效果,即使是之前正常工作的订单
    • 你是对的,它们确实有效,但是当我添加 AnnotationSecurityAspect 时,没有应用任何顺序。
    • AnnotationSecurityAspect 是 Spring 框架的一部分。我不知道它是如何编织到代码中的,也不知道它是否使用与用于您自己方面的编织器实例相同的编织器实例。您能否为我作为 Spring 菜鸟提供一个 Git(Hub) 存储库,其中包含一个可重现问题的现成项目?最好使用 Maven 构建?
    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    相关资源
    最近更新 更多