【问题记录】【SpringBoot】Filter中抛出的异常不会走RestControllerAdvice全局异常捕获

1  问题现象

//如下是我定义的全局异常捕获
@RestControllerAdvice public class RestExceptionHandler { /** * 默认全局异常处理。 * @param e the e * @return ResultDto */ @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public ResultDto<String> exception(Exception e) { ResultDto<String> resultDto = new ResultDto<>(); log.error("全局异常信息 ex={}", e.getMessage(), e); if (handleExceptionControl(e, resultDto)) { return resultDto; } resultDto.setCode(ResultCodeEnum.ERROR.getCode()); resultDto.setMsg(e.getMessage()); return resultDto; } } //这个是我定义的FIlter 用于校验用户是否登录 @Slf4j public class SsoTokenFilter extends HttpServlet implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; try { } catch (Exception e) { throw e;//不会走advice } } }

2  原因分析

请求进来,会按照 filter -> interceptor -> controllerAdvice -> aspect -> controller的顺序调用

当controller返回异常 也会按照controller -> aspect -> controllerAdvice -> interceptor -> filter来依次抛出

所以我们的Filter抛出的异常不会走advice,这是我们可以通过HandlerExceptionResolver来转换我们的异常。

3  解决办法

//自定义注解 引入SSO 登录校验Filter
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(SsoConfig.class}) public @interface EnableSsoLogin { } @Slf4j public class SsoConfig implements DisposableBean { @Autowired @Qualifier("handlerExceptionResolver") private HandlerExceptionResolver resolver; @Bean public FilterRegistrationBean ssoFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setName("SsoTokenFilter"); registration.setOrder(1); registration.addUrlPatterns("/*"); SsoTokenFilter ssoTokenFilter = new SsoTokenFilter(resolver); registration.setFilter(ssoTokenFilter); return registration; } } @Slf4j public class SsoTokenFilter extends HttpServlet implements Filter { private HandlerExceptionResolver resolver; public SsoTokenFilter(HandlerExceptionResolver resolver) { this.resolver = resolver; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; try {         //todo } catch(Exception e) { resolver.resolveException(req, res, null, e); } } }

  

 


posted @ 2022-12-26 19:42  酷酷-  阅读(810)  评论(0编辑  收藏  举报