SpringMVC-detailed interceptor process, use and customize interceptor

Source: Internet
Author: User

SpringMVC-detailed interceptor process, use and customize interceptor

 

First, let's look at what the interceptor does:

1. Logging: records the request information logs for information monitoring, information statistics, and calculation of PV (PageView.

2. Permission check: for example, if logon is detected, go to the processor check to check whether or not to log on. If not, go back to the logon page;

3. Performance monitoring: Sometimes the system is inexplicably slow for a certain period of time. You can use the Interceptor to record the start time before entering the processor, and record the end time after processing, so as to obtain the request processing time (if there is a reverse proxy, such as apache can automatically record );

4. Common behavior: Read the cookie to obtain user information and put the user object into the request, so as to facilitate subsequent use. For example, extract Locale and Theme information, the interceptor can be used as long as it is required by multiple processors.

5. OpenSessionInView: for example, Hibernate. after entering the processor to open the Session, close the Session.

In essence, it is also AOP (for Aspect-Oriented Programming). That is to say, all the functions that comply with the cross-cutting concerns can be implemented by the interceptor.

 

Spring provides us:

Org. springframework. web. servlet. HandlerInterceptor interface,

Org. springframework. web. servlet. handler. HandlerInterceptorAdapter adapter,

You can easily implement your own interceptor by implementing this interface or inheriting this class.

 

From the source code, we can see the relationship between interfaces and classes:

 


From the relationship, we can see that AsyncHandlerInterceptor inherits from HandlerInterceptor, while HandlerInterceptorAdapter implements AsyncHandlerInterceptor. In fact, this is a design mode called the adapter mode (you can click to view the adapter mode if you do not know it ).

If the HandlerInterceptor interface is implemented, all three methods must be implemented. If HandlerInterceptorAdapter is inherited, only the callback method we need can be implemented.

The HandlerInterceptor interface shows that the interceptor has three callback methods. Let's take a look at the functions of these three methods:



PreHandle:Preprocessing callback method to implement preprocessing of the processor (such as logon check). If the return value of the method is boolean, true indicates that the process continues. If false indicates that the process is interrupted, such as logon failure, in this way, no other interceptor will be called. We can use response to generate a response.

PostHandle:The post-processing callback method implements post-processing of the processor. This method is executed before the view is generated. In this method, you can modify modelAndView to process the model data or view.

AfterCompletion:The final execution can be used to release resources. Logs can be recorded based on whether ex is null and whether an exception has occurred.

The Object handler in the parameter is the next interceptor.

 

Custom Interceptor:

In the following example, after a user logs on, the user intercepts access to resources in the system through the interceptor and does not intercept related resources.

 

Public class LoginInterceptor extends HandlerInterceptorAdapter {private static final String [] IGNORE_URI = {/login. jsp,/Login/, backui/, frontui/}; @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {boolean flag = false; string url = request. getRequestURL (). toString (); System. out. println (>>:+ url); for (String s: IGNOR E_URI) {if (url. contains (s) {flag = true; break ;}} if (! Flag) {T_supplier_user user = LoginController. getLoginUser (request); if (user! = Null) flag = true;} return flag; // The process will be interrupted if false is returned.} @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {super. postHandle (request, response, handler, modelAndView );}}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.