spring-mvc-boot-9 interceptor

First, what Interceptors are?

When the request came DispatchServlet, according to HandlerMapping processor will find a mechanism to return HandlerExecutionChain object. This object contains a processor and interceptors. Blocker used to enhance the function of the processor.

  • Processor mapping --- [return] HandlerExecutionChain processor handler
  • The processor HandlerExecutionChain = + interceptors

Second, the interceptor interfaces

   public interface Handlerlnterceptor {
       //处理器执行前方法  boolean preHandle(request,response,handler)
       //处理器处理后方法  void  postHandle(request,response,handler,ModelAndView)
       //处理器完成后方法  void  afterCornpletion(request,response,handler,Exception)
   }
         +---------------+
         |   preHandle   |
         +-------+-------+
                 |
         +-------+-------+             +--------------+
         |    处理器     +-----------> |   postHandle |  +------------+
         +---------------+             +--------------+               |
                                                                      |
         +----------------+             +--------------+              |
         |afterCornpletion+<------------+   视图处理   |  <-----------+
         +----------------+             +--------------+

Third, the registration interceptor

      在配置文件中实现WebMvcConfigurer接口,最后覆盖其addlntereptors方法进行注册拦截器
        // 注册拦截器到Spring MVC机制,然后它会返回一个拦截器注册
        InterceptorRegistration ir = registry.addInterceptor(new Interceptor1());
        // 指定拦截匹配模式,限制拦截器拦截请求
        ir.addPathPatterns("/interceptor/*");

Fourth, the interceptor order

Sequential chain of responsibility the way. Be in the order of registration.

Guess you like

Origin blog.csdn.net/lidongliangzhicai/article/details/92772897