如何在spring aop中拦截aspect抛出的异常

有没有办法拦截异常并向最终客户显示有意义的消息?我正在尝试使用 spring AOP 授权我的 api,如果最终用户无权访问该 API,我将抛出异常。


@Aspect

public class AuthorizationAspect {

  @Pointcut("@annotation(AuthenticateAccount)")

    public void authorized() {}


   private boolean isAuthorized() {

   // logic to check is user is authorised to call the api

   }


    @Before("authorized()")

    public void beforeControllerCall(JoinPoint joinPoint) throws UnauthorizedException {


        if(!isAuthorized)) {

            throw new UnauthorizedException("You don't have rights over this API");

        }


    }

}

通过抛出异常,我能够阻止对 API 的访问,但它不会返回我试图抛出异常的有意义的消息。


有没有人处理过这样的用例并且可以帮助我解决这个问题?


至尊宝的传说
浏览 471回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java