spring全局异常处理针对spring security 异常处理失效问题

前言:项目使用@ControllerAdvice+@ExceptionHandler注解组合,实现全局异常处理器

@ExceptionHandler({AuthenticationException.class})
public Object handleAuthorizationException(HttpServletRequest request, AuthenticationException e)
    {
        log.error(e.getMessage(), e);
        if (ServletUtils.isAjaxRequest(request))
        {
            return AjaxResult.error(e.getMessage());
        }
        else
        {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("error/unauth");
            return modelAndView;
        }
}

问题:上述的 javax.security.sasl.AuthenticationException却拿不到,无法处理

原因:

sringsecurity是基于过滤器实现认证,针对异常的处理 ExceptionTranslationFilter 是 Spring Security 中专门负责处理异常的过滤器,默认情况下,这个过滤器已经被自动加载到过滤器链中
所以异常在到达controllerAdvice之前已被处理

针对上述异常,可以通过实现AuthenticationEntryPoint进行处理

此处借用一张图说明

 

 

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值