为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

至尊宝的传说 2021-08-19 17:41:18
有没有办法拦截异常并向最终客户显示有意义的消息?我正在尝试使用 spring AOP 授权我的 api,如果最终用户无权访问该 API,我将抛出异常。@Aspectpublic 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 的访问,但它不会返回我试图抛出异常的有意义的消息。有没有人处理过这样的用例并且可以帮助我解决这个问题?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 431 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信