The Spring MVC Interceptor (Handlerinterceptor) uses

Source: Internet
Author: User
Tags abstract auth xmlns
recently, not in the csdn to write articles, go to * Jane Book *, interested can pay attention to.

Today, Spring MVC's processor interceptors (Handlerinterceptor) are similar to filter filters in the servlet specification for preprocessing and post-processing of processors. Handlerinterceptor Interface

The Handlerinterceptor interface is defined as follows:

Package org.springframework.web.servlet;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.method.HandlerMethod;

Public interface Handlerinterceptor {

    /**
     * Preprocessing callback method
     * True indicates continuation process, FALSE indicates process interrupt */
    Boolean Prehandle (HttpServletRequest request, httpservletresponse response, Object handler)
            throws Exception;

    /**
     * post-processing callback method to implement post-processing of the processor (but before rendering the view), at which time the model data can be processed by modelandview or treated on the views
     */
    void Posthandle (
            HttpServletRequest request, HttpServletResponse response, Object handler, Modelandview Modelandview)
            throws Exception;

    /**
     * The entire request is processed by the callback method, that is, callback
    /void Aftercompletion (HttpServletRequest request) When the view is rendered
            . HttpServletResponse response, Object handler, Exception ex)
            throws Exception;

}

Spring MVC provides a Handlerinterceptoradapter adapter class that allows us to selectively implement the methods of interest, as follows:

Package Org.springframework.web.servlet.handler;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.web.servlet.AsyncHandlerInterceptor;

Import Org.springframework.web.servlet.ModelAndView; /** * Abstract Adapter class for the {@link Asynchandlerinterceptor} interface, * for simplified implementation of Pre-o
 Nly/post-only interceptors. * * @author Juergen Hoeller * @since 05.12.2003 * * Public abstract class Handlerinterceptoradapter implements Asynchand
     Lerinterceptor {/** * This implementation always returns {@code true}.
            */@Override public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler)
    Throws Exception {return true;
     }/** * This implementation is empty. */@Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object Handler, Modelandview modeLandview) throws Exception {}/** * This implementation is empty. */@Override public void aftercompletion (HttpServletRequest request, httpservletresponse response, Ob
     Ject handler, Exception ex) throws Exception {}/** * This implementation is empty. */@Override public void afterconcurrenthandlingstarted (HttpServletRequest request, Httpservletrespon SE response, Object handler) throws Exception {}}

The

is similar to Channelinboundhandler and Channelinboundhandleradapter in Netty4.1. Use

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs
       I= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <mvc:annotation-driven/> <context:com Ponent-scan base-package= "Com.bytebeats.springmvc.ch5.controller" > <context:include-filter type= "annotation "expression=" Org.springframework.stereotype.Controller "/> </context:component-scan> <mvc:intercepto
      Rs> <mvc:interceptor>      <mvc:mapping path= "/**"/> <bean class= "Com.bytebeats.springmvc.ch5.controller.interceptor.LogI Nterceptor "/> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path="/api/ * * "/> <mvc:exclude-mapping path="/api/auth/login "/> <mvc:exclude-mapping path="/api/ 
        Auth/register "/> <bean class=" Com.bytebeats.springmvc.ch5.controller.interceptor.AuthInterceptor "/> </mvc:interceptor> </mvc:interceptors> </beans>

Loginterceptor

Import Java.util.Set;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.web.servlet.HandlerInterceptor;


Import Org.springframework.web.servlet.ModelAndView; public class Loginterceptor implements handlerinterceptor{private Logger Logger = Loggerfactory.getlogger (loginterce

    Ptor.class); @Override public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handle
        R) throws Exception {if (condition) {return false;
    } return true; } @Override public void Posthandle (HttpServletRequest request, httpservletresponse response, Object H Andler, Modelandview Modelandview) throws Exception {} @Override public void Aftercompletion (Ht
            Tpservletrequest request,HttpServletResponse response, Object handler, Exception ex) throws Exception {}} 
Application ScenariosLog records: Log the request information for information monitoring, information statistics, calculation pv,trace tracking, and so on. Permission check: such as login detection, enter the processor detection check whether to log in, if not directly back to the login page; performance monitoring: Sometimes the system in a certain period of time inexplicably slow, can pass through the interceptor before entering the processor before the start time, in processing the PostScript;

The internal implementation principle of Handlerinterceptor is described in detail in the next article.

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.