跟进我上一个问题的评论。我试图从@Around
建议中抛出一个异常,并在被调用者类和/或方法中捕获它。但我收到了这个错误:
Stacktraces
java.lang.Exception: User not authorized
com.company.aspect.AuthorizeUserAspect.isAuthorized(AuthorizeUserAspect.java:77)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:616)
...
方面代码是:
@Aspect
public class AuthorizeUserAspect {
@AuthoWired
private UserService service;
@Pointcut("@annotation(module)")
public void authorizeBeforeExecution(AuthorizeUser module) {}
@Around(value = "authorizeBeforeExecution(module)", argNames="module")
public Object isAuthorized(ProceddingJoinPoint jp, AuthorizeUser module) throws Throwable {
// Check if the user has permission or not
service.checkUser();
if ( /* User has NOT permission */ ) {
throw new MyCustomException("User not authorized"); // => this is line 77
}
return jp.proceed();
}
}
而基于 Struts 的 UI 动作代码是:
@Component
public class DashboardAction extends ActionSupport {
@Override
@AuthorizeUser
public String execute() {
...
}
private void showAccessDenied() {
...
}
}
问题是如何或在哪里可以捕获该异常以执行showAccessDenied()
?