SpringMVC Interceptor
Interceptor for Spring MVC is similar to Filter for Servlet. It is mainly used to intercept user's requests and handle them accordingly. It is often used for privilege validation, logging request information, determining whether user is logged in, etc.
Differences between interceptors and filters
- Interceptor SpringMVC component, and filter is a Servlet component
- Interceptors do not rely on containers, filters on containers
- Interceptors can only work on controller requests, while filters can work on all requests
- Interceptors can get individual bean s in IOC containers, but filters are not very convenient
Define Interceptors
Defining an interceptor in Spring MVC requires the creation and configuration of the interceptor. The HandlerInterceptor interface needs to be implemented when creating interceptors
Introducing interceptor methods
There are three abstract methods in the HandlerInterceptor interface that represent the point of interception
- preHandle method:
This method is executed before the controller's processing request method, and its return value indicates whether subsequent operations are interrupted, its return true indicates to continue executing down, and its return false indicates to interrupt subsequent operations - PosHandle method:
This method is executed after the controller's processing request method is executed and before the view is resolved, which allows further modifications to the models and views in the request domain - afterCompletion method
This method is executed after the controller's processing request method is executed, that is, after the rendering of the view is completed. It can be used to clean up some resources, record log information, and so on.
Create Interceptor
public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("ProHandle: Before the target method that handles the request executes"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("postHandle: After the target method executes, before the view parser processes the view"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("afterCompletion: Before the client responds."); } }
Configuring Interceptors
Interceptors need to be configured with the <mvc:interceptors>tag in the SpringMVC configuration file
<!-- Configuring Interceptors--> <mvc:interceptors> <mvc:interceptor> <!-- mapping:Configure Interceptor Action Path--> <mvc:mapping path="/**"/> <!--Configuring an action path that does not require interception--> <mvc:exclude-mapping path="/user/getUsers"/> <bean class="com.lanh.interceptor.MyInterceptor"/> </mvc:interceptor> </mvc:interceptors>
Define Global Interceptors
A global interceptor is one that intercepts all URL s processed by the controller. Equivalent to /**
Create Global Interceptor
public class GlobalInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("Global PreHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("Global PostHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("Global AfterCompletion"); } }
Configure Global Interceptors
<!-- Configuring Interceptors--> <mvc:interceptors> <!--Configure Global Interceptors--> <bean class="com.lanh.interceptor.GlobalInterceptor"/> <mvc:interceptor> <!-- mapping:Configure Interceptor Action Path--> <mvc:mapping path="/**"/> <!--Configuring an action path that does not require interception--> <!--<mvc:exclude-mapping path="/user/getUsers"/>--> <bean class="com.lanh.interceptor.MyInterceptor"/> </mvc:interceptor> </mvc:interceptors>
Multiple Interceptor Execution Order
If a URL can be intercepted by more than one interceptor, the global interceptor executes first, while the other interceptors determine the order of execution based on the order of up and down configurations in the configuration file. Who configures first, who executes first
<!-- Configuring Interceptors--> <mvc:interceptors> <!--Configure Global Interceptors--> <bean class="com.lanh.interceptor.GlobalInterceptor"/> <mvc:interceptor> <!-- mapping:Configure Interceptor Action Path--> <mvc:mapping path="/**"/> <!--Configuring an action path that does not require interception--> <!--<mvc:exclude-mapping path="/user/getUsers"/>--> <bean class="com.lanh.interceptor.MyInterceptor"/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.lanh.interceptor.MyInterceptor2"/> </mvc:interceptor> </mvc:interceptors>