SpringBoot对Filter过滤器中的异常进行全局处理方案详解

  @Component

  public class MyFilter implements Filter {

  @Override

  public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {

  }

  @Override

  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

  //...进行一些逻辑处理

  if (...) {

  HandlerExceptionResolver handlerExceptionResolver = SpringContextUtil.getBean("handlerExceptionResolver");

  handlerExceptionResolver.resolveException((HttpServletRequest) servletRequest, (HttpServletResponse) servletResponse,

  null,new ServiceException("抛出过滤器全局异常!"));

  return;

  }

  filterChain.doFilter(servletRequest, servletResponse);

  }

  @Override

  public void destroy() {

  }

  }