SpringBoot use Filter and Interceptor

A, Filter (filter)

Filter interface is defined in javax.servletthe package, is defined Servlet specification, acting on the front and rear Request / Response, it is invoked Servlet container, when the managed Sring Filter Spring container resources may be used.

Achieve a Filter

Custom filters need to achieve javax.servlet.Filter, Filter interface has three methods:

  • init(FilterConfig filterConfig): Filter initialization is called.

  • doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain): In the doFilter()process, chain.doFilter()before the filtering operation is generally performed on the request, chain.doFilterthe latter is an operation of the response code is usually performed chain.doFiter()next to execute a filter or a service processor.

  • destory(): When the filter is called destruction.

Filters used in the Spring container

  • By FilterRegistrationBean
 @Configuration
 public class WebConfig{
    @Bean
    public FilterRegistrationBean xxxFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new xxxFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/*"));
        registrationBean.setOrder(1); // 过滤器的优先级
        return registrationBean;
    }
}    
复制代码
  • By @WebFilterand@ServletComponentScan

By @WebFilterway of definition of Filter, Filter class name defaults to set priorities. Use FilterRegistrationBeancan assign priorities. Filter whitelist filtering Url embodiment, the configuration requires Url intercept, if you want to set the filter is not required in certain Url doFilterspecified method.

Two, Interceptor (Interceptor)

Interceptor need to achieve a defined org.springframework.web.servlet.HandlerInterceptorinterfaces, container Spring Interceptor is defined, it can use any resource Spring container, as long as can be injected into Interceptor through IoC, Interceptor can drill down to perform before and after the service processing method and throw an exception of the time, and Filerter can not do this, so compared to the Interceptor Filter greater flexibility.

Implement a Interceptor

Implement HandlerInterceptoror inheritHandlerInterceptorAdapter

public interface HandlerInterceptor {
   default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       return true;
   }

   default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
   }

   default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
   }
}
复制代码
  • preHandle: service is called before requesting processor.

  • postHandle: After the service request and generates a processor is called a front view, then the opportunity to modify ModelAndView.

  • afterCompletion: After processed the business process request (the view has been rendered) is performed, and the method can handle traffic scene exception occurred.

Use Spring container interceptors

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new TimeInterceptor());
        registration.excludePathPatterns("/user");
        registration.excludePathPatterns("/*");
    }
}    
复制代码

Interceptor can either specify to filter Url can also specify not intercept Url, default intercept all Url.

Third, the calling sequence

Reproduced in: https: //juejin.im/post/5d064bc0e51d4510aa0114f5

Guess you like

Origin blog.csdn.net/weixin_34413802/article/details/93176396