Spring MVC interceptor _ Power node Java College collation

  • 2020-09-16 07:28:12
  • OfStack

Spring provides us with:

org. springframework. web. servlet. HandlerInterceptor interface,
org. springframework. web. servlet. handler. HandlerInterceptorAdapter adapter,

Implementing this interface or inheriting from this class makes it very easy to implement your own interceptors.

Here are three ways to do it:

Action before execution:


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

Execute before the view is generated


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

Finally executed, which can be used to release resources


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

Pre-processing, post-processing (called Service and returned ModelAndView, but not rendered), and return processing (rendered page) are implemented respectively.

In preHandle, you can code, security control and other processing;

In postHandle, there is an opportunity to modify ModelAndView;

In afterCompletion, you can determine whether an exception has occurred based on whether ex is null and log it.

The Object handler parameter is the next interceptor.

How do I use interceptors?

Customize 1 interceptor to implement HandlerInterceptor interface:

Java code


public class MyInteceptor implements HandlerInterceptor {   
   A little...  
}

Spring MVC does not have a total interceptor and cannot intercept all requests back and forth.

The interceptor of Spring MVC, which belongs to the HandlerMapping class, can have multiple HandlerMapping, each HandlerMapping can have its own interceptor.

When a request executes the implementation class of the HandlerMapping interface in order according to the value of Order from small to large, which one returns first, then it is finished. The following HandlerMapping will not go, and the procedure is completed. So let's go to the next step.

When will the interceptor execute? When a request is handed over to an HandlerMapping, the HandlerMapping first looks for a processor to handle the request, and when it finds it, it executes the interceptor. After executing the interception, it is handed over to the target processor.

If no processor is found, then the interceptor will not be executed.

There are three ways to configure in the configuration file for spring MVC:

Scenario 1, (approximate) total interceptor, intercepts all url

Java code


  <mvc:interceptors> 
  <bean class="com.app.mvc.MyInteceptor" /> 
</mvc:interceptors> 

Why is it called "approximate"? As mentioned earlier, Spring has no total interceptor.

<mvc:interceptors/> One interceptor is injected for every HandlerMapping. There will always be 1 HandlerMapping that can find the processor, or at most 1 processor, so the interceptor will always be executed. It ACTS as a total interceptor.

If it is ES105en-style URL, static resources will also be blocked.

Scheme 2, (approximate) total interceptor, intercepts matching URL.

Xml code


<mvc:interceptors >  
 <mvc:interceptor>  
    <mvc:mapping path="/user/*" /> <!-- /user/* -->  
    <bean class="com.mvc.MyInteceptor"></bean>  
  </mvc:interceptor>  
</mvc:interceptors>  

That's one more URL match than option 1.

If it is URL, REST style, static resources will also be blocked.

Scenario 3, interceptors on HandlerMappint.

If it is ES128en-style URL, static resources will not be intercepted. Because we injected interceptors with precision.

Xml code


<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">    
 <property name="interceptors">    
   <list>    
     <bean class="com.mvc.MyInteceptor"></bean>   
   </list>    
 </property>    
</bean>  

If you use <mvc:annotation-driven />,  It automatically registers bean and AnnotationMethodHandlerAdapter, so there is no chance to inject the interceptors property into it, and the interceptor cannot be specified.

Of course we can manually configure the above two Bean without using it < mvc:annotation-driven / > , you can inject the interceptor into the interceptors property.

I don't recommend it either <mvc:annotation-driven />, It is recommended to write detailed configuration files manually instead <mvc:annotation-driven /> You have a lot of control.

How to replace <mvc:annotation-driven /> ? What on earth did he do?

1 words <mvc:annotation-driven /> The following is actually done :(not including adding your own defined interceptors)

Once we know this, we have more control over Spring3 MVC, and we can change it wherever we want.

Xml code


 <!--  Annotation request mapping  --> 
  <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">     
  <property name="interceptors"> 
    <list>  
      <ref bean="logNDCInteceptor"/>  <!--  Log interceptor, this is your custom interceptor  --> 
      <ref bean="myRequestHelperInteceptor"/>  <!-- RequestHelper Interceptor, this is your custom interceptor -->  
      <ref bean="myPermissionsInteceptor"/> <!--  Permission interceptor. This is your custom interceptor -->  
      <ref bean="myUserInfoInteceptor"/> <!--  User information interceptor. This is your custom interceptor -->  
    </list>     
  </property>     
</bean>   
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
  <property name="messageConverters">  
    <list>  
      <ref bean="byteArray_hmc" />  
      <ref bean="string_hmc" />  
      <ref bean="resource_hmc" />  
      <ref bean="source_hmc" />  
      <ref bean="xmlAwareForm_hmc" />  
      <ref bean="jaxb2RootElement_hmc" />  
      <ref bean="jackson_hmc" />  
    </list>  
  </property>  
</bean>  
<bean id="byteArray_hmc" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /><!--  To deal with .. --> 
<bean id="string_hmc" class="org.springframework.http.converter.StringHttpMessageConverter" /><!--  To deal with .. --> 
<bean id="resource_hmc" class="org.springframework.http.converter.ResourceHttpMessageConverter" /><!--  To deal with .. --> 
<bean id="source_hmc" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /><!--  To deal with .. --> 
<bean id="xmlAwareForm_hmc" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /><!--  To deal with .. --> 
<bean id="jaxb2RootElement_hmc" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" /><!--  To deal with .. --> 
<bean id="jackson_hmc" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /><!--  To deal with json--> 


Related articles: