I. Filter

The Filter interface is defined in javax.servlet package, which is defined by the Servlet specification. It functions before and after Request/Response and is called by the servlet container.

Implementing a Filter

Custom filters need to implement javax.servlet.Filter. There are three methods in the Filter interface:

  • Init (FilterConfig FilterConfig) : the filter initialization is called.

  • DoFilter (ServletRequest ServletRequest, ServletResponse ServletResponse, FilterChain chain) : In the doFilter() method, chain-.dofilter () is used to filter requests, chain-.dofilter is used to filter response, and chain-.dofiter () is used to filter the next filter or service processor.

  • Destory () : called when the filter is destroyed.

Use filters in the Spring container

  • Through FilterRegistrationBean
 @Configuration
 public class WebConfig{
    @Bean
    public FilterRegistrationBean xxxFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new xxxFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/ *")); registrationBean.setOrder(1); // Filter priorityreturnregistrationBean; }}Copy the code
  • through@WebFilterand@ServletComponentScan

You can define a Filter as @webFilter. By default, the Filter class name is used to set the priority. The priority can be specified using the FilterRegistrationBean. Filter Whitelist is used to Filter urls. Specify the urls to be blocked. If you want to exclude certain urls, you need to specify this parameter in the doFilter method.

Interceptor = Interceptor = Interceptor

Define an Interceptor to implement org. Springframework. Web. Servlet. HandlerInterceptor interface, the Interceptor is defined the Spring container, it can use the Spring container of any type of resource, As long as it is injected into the Interceptor through IoC, the Interceptor can go deep into the execution of a business process method and when it throws an exception. Filerter cannot do this, so the Interceptor is more flexible than the Filter.

Implement an Interceptor

Implement HandlerInterceptor or HandlerInterceptorAdapter inheritance

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 {
   }
}
Copy the code
  • PreHandle: Called before the request is processed by the business processor.

  • PostHandle: Called after the business processor has processed the request and before the view is generated, at which point it has the opportunity to modify the ModelAndView.

  • AfterCompletion: After the business processing processor has processed the request (the rendered view), it is executed and can handle the scenario in which the business method fails.

Use interceptors in the Spring container

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration registration = registry.addInterceptor(new TimeInterceptor());
        registration.excludePathPatterns("/user");
        registration.excludePathPatterns("/ *"); }}Copy the code

The Interceptor can specify either the Url to be filtered or the Url not to be blocked. By default, all urls are blocked.

Call order