0

我刚开始学习 AspectJ,我有一个用例,比如用户登录。如果用户的会话数据(cookies)与服务器上存储的数据不匹配,我想更改调用的函数。假设我有两个操作:

class HttpServlet { 
   public function() { 
   } 
   public function2() { 
   }
   public doLogin() { 
   }
} 

我有这样的建议:

public aspect UserLoggedIn {

    pointcut GreetingServer(): within(HttpServlet);
    pointcut requireAuth(): 
       GreetingServer() && execution(* function*(..));
    before(): requireAuth() {
        if ( notLoggedIn ) { 
          redirectToDoLoginAndAbortCalledFunction();
        }
    }
}

那么如何使 redirectToDoLoginAndAbortCalledFunction() 工作?

4

2 回答 2

3

You will want to use around advice instead of before advice, something like below. Here is an example that assumes that both methods return boolean:

 boolean around(): requireAuth() {
    if ( notLoggedIn ) {           
        return redirectToDoLoginAndAbortCalledFunction();        
    } else {
        return proceed();
    }    
 }

You may also need to pass in parameters to your advice, which you can do by capturing the proper values in the pointcut using the this(), target(), and args() pointcuts.

于 2009-09-12T22:24:23.607 回答
2

在我们的项目中,我们使用 servlet Filter 来实现完全相同的身份验证目的。你有什么理由要为此使用 AOP 吗?

但是如果您仍然需要为此使用 AspectJ,您应该使用around方面以便能够干扰方法调用。我们使用类似的技术来缓存方法返回值。

You can look at this article for an example http://www.theserverside.com/tt/blogs/showblog.tss?id=AspectJCaching.

于 2009-09-03T08:26:29.773 回答