2011-01-30 9 views
2

をoverwrittingためAspectJの宣言的な構文は、これは注釈の構文で前に回答されているされていますAspectj overwrite an argument of a methodは、どのような引数

しかし、私は、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(); 
    } 

} 

すべての引数を "proceed()"する方法はわかりません。

答えて

6

私は注釈バージョンの作業を得た:

@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); 
    } 
} 

は今、私はすべての私のSpring MVCコントローラのXSS保護を持っています。私はaspectJの構文が働くことを望んでいた。

+0

従来のAspectJ構文では、前後のアドバイスでproceed()を呼び出したときに、そのポイントカットで収集されたコールと一致しています。各文字列引数を更新する必要がある場合、@AspectJ構文が唯一の選択肢です。 – ramnivas

+0

@ramnivas私の疑惑を確認していただきありがとうございます。あなたの本は特に3章です。 –

+0

私の場合(Spring 3.2)、RequestMappingアノテーションのフルネームを指定する必要がありました。つまり、次のようになります。 @Around( "execution(@ org.springframework.web .bind.annotation.RequestMapping * *(..)) ") –

0

return thisJoinPoint.proceed(args);は何をしますか?

+0

これは、注釈AspectJ構文でのみ機能します。 –

+1

あなたはそうです。私はあなたが宣言的にそれを行うことはできないと思う - args()は引数を扱うのに十分柔軟ではない。 – Howard

関連する問題