HandlerInterceptor拦截器(Handlerinterceptor interceptor)

HandlerInterceptor拦截器

拦截器

什么是拦截器:在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略

为什么需要拦截器:在做身份认证或者是进行日志的记录时,我们需要通过拦截器达到我们的目的。最常用的登录拦截、或是权限校验、或是防重复提交、或是根据业务像12306去校验购票时间,总之可以去做很多的事情

如何用拦截器:在spring中用拦截器需要实现HandlerInterceptor接口或者它的实现子类:HandlerInterceptorAdapter,同时在applicationContext.xml文件中配置拦截器

实现方法

1、类要实现Spring 的HandlerInterceptor 接口

2、类继承实现了HandlerInterceptor 接口的类,例如 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter

3.在实现中添加拦截器,执行顺序为添加顺序,或者在添加时候指定order值越小优先级越高。

WebMvcConfigurer配置接口类

HandlerInterceptor方法

boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception;

void postHandle(
		HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
		throws Exception;

void afterCompletion(
		HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
		throws Exception;

preHandle:在业务处理器处理请求之前被调用。预处理,可以进行编码、安全控制、权限校验等处理;

postHandle:在业务处理器处理请求执行完成后,生成视图之前执行。后处理(调用了Service并返回ModelAndView,但未进行页面渲染),有机会修改ModelAndView (这个博主就基本不怎么用了);

afterCompletion:在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。返回处理(已经渲染了页面);

HandlerInterceptor执行顺

假如有拦截器1、2、3,preHandle方法中打印pre1、pre2、pre3,postHandle方法中打印post1、post2、post3,afterCompletion方法中打印after1、after2、after3,如果全部拦截器都执行执行结束,打印结果应该为:

pre1, pre2, pre3, post3, post2, post1, after3, after2, after1

————————

HandlerInterceptor拦截器

Interceptor

< strong > what is an interceptor < / strong >: in AOP (aspect oriented programming), it is used to intercept a method or field before it is accessed, and then add some operations before or after it. Interception is an implementation strategy of AOP

< strong > Why do we need interceptors < / strong >: we need to use interceptors to achieve our goals in identity authentication or log recording. The most commonly used login interception, or permission verification, or anti duplicate submission, or checking the ticket purchase time according to the business like 12306. In short, you can do a lot of things

< strong > how to use interceptors < / strong >: to use interceptors in spring, you need to implement the handlerinterceptor interface or its implementation subclass: handlerinterceptoradapter, and in ApplicationContext Configuring interceptors in XML files

Implementation method

1. Class to implement the handlerinterceptor interface of spring

2. Class inherits classes that implement the handlerinterceptor interface, such as the provided abstract class handlerinterceptoradapter that implements the handlerinterceptor interface

3. Add interceptors in the implementation in the order of adding, or specify that the smaller the order value, the higher the priority.

WebMvcConfigurer配置接口类

HandlerInterceptor方法

boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
		throws Exception;

void postHandle(
		HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
		throws Exception;

void afterCompletion(
		HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
		throws Exception;

Prehandle: called before the business processor processes the request. Preprocessing, including encoding, security control and permission verification;

Posthandle: execute after the business processor processes the request and before generating the view. Post processing (calling service and returning modelandview without page rendering), and having the opportunity to modify modelandview (this blogger doesn’t use it very much);

After completion: it is called after the dispatcher servlet has completely processed the request, which can be used to clean up resources, etc. Return to processing (the page has been rendered);

HandlerInterceptor执行顺

If there are interceptors 1, 2 and 3, pre1, pre2 and pre3 are printed in the prehandle method, POST1, post2 and post3 are printed in the posthandle method, and after1, after2 and after3 are printed in the aftercompletion method. If all interceptors are executed, the print result should be:

pre1, pre2, pre3, post3, post2, post1, after3, after2, after1