1

私は AOP に少し慣れていないので、直面している問題について混乱しています。@AuthorizeUserプレゼンテーション層のメソッドに作用する注釈があります。ユーザーがそのメソッドを実行する権限があるかどうかを確認する必要があります。のコードは次のAuthorizeUserAspectとおり です。

@Aspect
public class AuthorizeUserAspect {
    @AuthoWired
    private UserService service;

    @Before(value = "@annotation(com.company.annotation.AuthorizeUser)")
    public void isAuthorized(JoinPoint jp) {
        // Check if the user has permission or not
        // executing some Service Layer services and 
        // Persistence Layer, corresponding to that
        service.checkUser();

        // Is there a way I can make this method Conditional. something like:
        if ( /* User has permission */ ) {
            // do nothing, so the method will be executed after this
        }
        else {
            // 1) Prevent the Method to be executed [and/or]
            // 2) Pass some Parameters to the method for checking [and/or]
            // 3) Execute another method on that class [e.g showAccessDenied()]
        }
    }
}

この質問Spring MVC + Before Advice check securityに少し似ています。ただし、文字列を返すことを提案しました(つまり、「Not OK」)。私のアプリケーションには 2 種類の UI (Struts と Jersey) があるため、2 種類の戻り値の型 (StringResponseそれぞれ) があります。ですから、それが最善の方法ではないかもしれないと思います。

この場合の回避策を教えていただければ幸いです。
これは良いアプローチですか?

4

1 に答える 1

2

まず、Spring Securityを見ましたか? これは完全に宣言型であり、自分でアスペクトを記述する必要はありません。ユーザーが認証されていないか、必要な権限を持っていない場合は、例外をスローしてメソッドを保護します。

2 つの異なる戻り値の型に関する問題について:

最初のオプション: メソッドの戻り値の型に固有の 2 種類のアドバイスを作成します。

@Before("@annotation(com.company.annotation.AuthorizeUser) && execution(String *.*(..))")
public void isAuthorizedString(JoinPoint jp) {
    ...
}

@Before("@annotation(com.company.annotation.AuthorizeUser) && execution(Response *.*(..))")
public void isAuthorizedResponse(JoinPoint jp) {
    ...
}

2 番目のオプション: リフレクションを介して推奨されるメソッドの戻り値の型を見つけ、それに基づいて別の値を返します。

@Before("@annotation(com.company.annotation.AuthorizeUser")
public void isAuthorized(JoinPoint jp) {
    Class<?> returnType = ((MethodSignature)jp.getStaticPart()
            .getSignature()).getReturnType();
    if(returnType == String.class)
        ...
    else
        ...
}
于 2012-09-03T14:13:24.230 に答える