Spring Boot implements an interceptor that listens for user requests

Source: Internet
Author: User

The project needs to listen to the user specific request operation, then through an interceptor to listen to, and continue the corresponding logging
Project building with Spring boot,spring Boot implementation of an interceptor is easy.

Spring Boot's core boot class inherits Webmvcconfigureradapter

    Add interceptors    @Override public    void Addinterceptors (Interceptorregistry registry) {        Registry.addinterceptor ( New Requestlog ());    }   This requestlog is the interceptor we've defined.

The Interceptor is written

public class Requestlog extends Handlerinterceptoradapter {/** * Front check */@Override public boolean Prehan Dle (HttpServletRequest request, httpservletresponse response, Object handler) throws Exception {String        ip = request.getremoteaddr ();        Long startTime = System.currenttimemillis ();        Request.setattribute ("Requeststarttime", startTime);        Handlermethod Handlermethod = (handlermethod) handler;        Get User Token Method method = Handlermethod.getmethod ();        System.out.println ("User:" +ip+ ", Access target:" +method.getdeclaringclass (). GetName () + "." + Method.getname ());    return true; }    //Controller Processing Completepublic void Posthandle (HttpServletRequest request, httpservletresponse response, Object handler, Modelandview m        Odelandview) throws Exception {Handlermethod Handlermethod = (handlermethod) handler;        Method method = Handlermethod.getmethod ();        Long startTime = (long) request.getattribute ("Requeststarttime");        Long endTime = System.currenttimemillis ();        Long executetime = Endtime-starttime; Log it if (Executetime > +) {System.out.println ("[" + Method.getdeclaringclass (). GetName () + "        . "+ method.getname () +"] Execution time: "+ executetime +" MS ");  } else {System.out.println ("[" + Method.getdeclaringclass (). Getsimplename () + "." + method.getname () + "] Execution time-consuming        : "+ executetime +" MS "); }    }}

The interceptors we implement ourselves need to inherit handlerinterceptoradapter and rewrite the following three methods:

In the prehandle , can encode , security control and other processing, in Posthandle , have the opportunity to modify Modelandview;  

There is also a more important way to do this in aftercompletion, the following are some of the three methods that are executed:

initiate the request, enter the interceptor chain, and run the Prehandle method of all interceptors.
when the Prehandle method returns False, ( the following interceptor no longer executes ) executes the aftercompletion method of all interceptors from the current interceptor (not Posthandle method ), then exit the Interceptor chain.

when the Prehandle method is all true, the next interceptor is executed until all interceptors are executed. Run the intercepted controller again. Then go into the interceptor chain, run the Posthandle method of all interceptors, and execute the Aftercompletion method of all interceptors back from the last interceptor.
When an interceptor throws an exception, the Aftercompletion method of all interceptors is executed back from the current interceptor


Prehandle Method:
Returns TRUE, the map processor execution chain will continue to execute;
When False is returned, the Dispatcherservlet processor considers that the interceptor has processed the request (the request has been intercepted) and does not continue executing other interceptors and processors in the execution chain.

Http://www.itdadao.com/articles/c15a591762p0.html

Spring Boot implements an interceptor that listens for user requests

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.