Code sharing of login interceptor implemented by spring MVC

I've been in contact with struts interceptors before, but I haven't used spring MVC interceptors. I spent a day studying them today.

This paper first introduces the basic concept of interceptor, then expounds the brief difference between interceptor and filter, realizes the interceptor function through three methods defined in handlerinterceptor interface, and finally introduces the configuration related code. Next, let's see the specific content.

Define interceptor

Interceptor in spring MVC intercepts requests through handlerinterceptor. Defining an interceptor in spring MVC is very simple. There are two main ways. The first way is that the interceptor class to be defined implements the handlerinterceptor interface of spring, or this class inherits the class that implements the handlerinterceptor interface, such as the abstract class handlerinterceptoradapter that implements the handlerinterceptor interface provided by spring; The second way is to implement the webrequestinterceptor interface of spring, or inherit the class that implements webrequestinterceptor.

Usage scenario of Interceptor: solve the problem of garbled code and permission verification. Usage principle: deal with the common problems encountered by all requests

The difference between interceptors and filters

The filter depends on the servlet container. It is based on the callback function and has a wide filtering range

The interceptor relies on the framework container and only filters requests based on the reflection mechanism

Interceptors can handle some common problems of requests in web applications

Common problems are handled in the interceptor, which can reduce duplicate code and facilitate maintenance

Implement the handlerinterceptor interface

Three methods are defined in the handlerinterceptor interface. We use these three methods to intercept user requests.

(1) Prehandle (HttpServletRequest request, httpservletresponse, object handle) method. As the name suggests, this method will be called before request processing. Interceptors in spring MVC are called in a chain. Multiple interceptors can exist simultaneously in an application or in a request. Each interceptor call will be executed in sequence according to its declaration order, and the first execution is the prehandle method in the interceptor. Therefore, some pre initialization operations or preprocessing of the current request can be carried out in this method, or some judgments can be made in this method to determine whether the request should continue. The return value of this method is of boolean type. When it returns false, it means that the request ends and subsequent interceptors and controllers will not execute again; When the return value is true, it will continue to call the prehandle method of the next interceptor. If it is the last interceptor, it will call the controller method of the current request.

(2) The posthandle (HttpServletRequest request, object handle, modelandview, modelandview) method is explained by the prehandle method. We know that this method, including the aftercompletion method to be mentioned later, can only be called when the return value of the prehandle method of the current interceptor is true. As the name suggests, the posthandle method is executed after the current request is processed, that is, after the controller method is called, but it will be called before the dispatcher servlet renders the view return, so we can operate the modelandview object after the controller processing in this method. The posthandle method is called in the opposite direction to the prehandle, that is, the posthandle method of the interceptor declared first will be executed later, which is somewhat different from the execution process of the interceptor in struts 2. The execution process of the interceptor in struts 2 is also chained, but in struts 2, you need to manually call the invoke method of actioninvocation to trigger the call to the next interceptor or or action. Then, the contents before the invoke method call in each interceptor are executed in the declared order, and the contents after the invoke method are reversed.

(3) Aftercompletion (HttpServletRequest request, exception Ex) method, which is also executed only when the return value of the prehandle method of the current corresponding interceptor is true. As the name suggests, this method will be executed after the end of the entire request, that is, after the dispatcher servlet renders the corresponding view. The main function of this method is to clean up resources.

I tried to do a simple login interception to judge whether there is information in the session The following is the code. The code is very simple. Judge whether the user name and password are correct in the controller, and then save the user information to the session. The method is as follows:

Interceptors are as follows:

After the interceptor is completed, it needs to be configured in the configuration file,

The configuration is as follows:,

summary

The above is all about the code sharing of the login interceptor implemented by spring MVC. I hope it will be helpful to you. Interested friends can continue to refer to this site: introduction to spring MVC using multipartfile to realize asynchronous upload, spring MVC executing method source code analysis after startup, etc. If there are deficiencies, please leave a message to point out. Xiaobian will reply to you in time and make modifications. Thank you for your support of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>