Java 用于覆盖参数的AspectJ声明性语法是什么

Java 用于覆盖参数的AspectJ声明性语法是什么,java,aspectj,Java,Aspectj,这一点以前已经用注释语法得到了回答: 但我不知道如何使用AspectJ声明性语法。 下面应该在方法中的每个字符串前面添加“Poop”,但它没有 public aspect UserInputSanitizerAdvisor { pointcut unSafeString() : execution(@RequestMapping * * (..)); Object around() : unSafeString() { //thisJoinPoint.getA

这一点以前已经用注释语法得到了回答:

但我不知道如何使用AspectJ声明性语法。 下面应该在方法中的每个字符串前面添加“Poop”,但它没有

public aspect UserInputSanitizerAdvisor {

    pointcut unSafeString() : execution(@RequestMapping * * (..));

    Object around() : unSafeString() {
        //thisJoinPoint.getArgs();
        //proceed();
        System.out.println("I'm Around");
        Object[] args = thisJoinPoint.getArgs();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                Object o = args[i];
                if (o != null && o instanceof String) {
                    String s = (String) o;
                    args[i] = "poop: " + s;
                }
            }
        }

        return proceed();
    }

}
public方面用户输入SanitizeRadvisor{
切入点不安全():执行(@RequestMapping**(..);
对象周围():不安全(){
//thisJoinPoint.getArgs();
//继续();
System.out.println(“我在附近”);
Object[]args=thisJoinPoint.getArgs();
如果(args!=null){
对于(int i=0;i

我不知道如何给出“procedure()”所有参数。

是否返回此连接点。procedue(args)做你想做的事?

我让注释版本正常工作:

@SuppressWarnings("unused")
@Aspect
public class UserInputSanitizerAdivsor {

    @Around("execution(@RequestMapping * * (..))")
    public Object check(final ProceedingJoinPoint jp) throws Throwable {
        Object[] args = jp.getArgs();
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                Object o = args[i];
                if (o != null && o instanceof String) {
                    String s = (String) o;
                    args[i] = UserInputSanitizer.sanitize(s);
                }
            }
        }
        return jp.proceed(args);
    }
}
@SuppressWarnings(“未使用”)
@面貌
公共类UserInputSanitizerAdivsor{
@大约(“执行(@RequestMapping**(..)”)
公共对象检查(最终处理连接点jp)抛出可丢弃{
对象[]args=jp.getArgs();
如果(args!=null){
对于(int i=0;i

现在我的所有Spring MVC控制器都有XSS保护。不过,我希望aspectJ语法能够正常工作。

传统的aspectJ语法强制在一个around通知中调用made to procedure(),与它的切入点收集的结果相匹配。鉴于您需要更新每个字符串参数,@AspectJ语法是唯一真正的选择。@ramnivas感谢您确认我的怀疑。你的书非常棒,尤其是第3章。在我的例子(Spring 3.2)中,我必须指定RequestMapping注释的全名才能让它工作,比如:@Around(“execution(@org.springframework.web.bind.annotation.RequestMapping**(..))你说得对。我不认为你可以用声明的方式来做->args()不够灵活,无法处理任何参数。