Introduction to Spring Interceptor (Interceptor)

Source: Internet
Author: User

1. Interceptor Use

(1) block non-logged users to access certain links directly

(2) Intercept log messages

(3) Interception of illegal attacks, such as SQL injection

2. Involving JAR, class

(1) Spring-webmvc.jar

(2) Handlerinterceptor (Org.springframework.web.servlet: interface),

Asynchandlerinterceptor (Org.springframework.web.servlet: interface),

Handlerinterceptoradapter (Org.springframework.web.servlet.handler.HandlerInterceptorAdapter: abstract Class)

3. Business Class

(1) Implement (implements) implement Handlerinterceptor interface or sub-interface

(2) Inheritance (extends) Inheritance Handlerinterceptor interface Subclass (abstract class)

(3) The methods involved

Prehandle, Posthandle, aftercompletion

4. Test code

Logininterceptor

1  Public BooleanPrehandle (httpservletrequest request, httpservletresponse response, Object handlerthrowsException {2Logger.info ("--------prehandle-------" +Request.getrequesturi ());3HttpSession session =request.getsession ();4String login_account = string.valueof (Session.getattribute (commonconstants.session_key+Session.getid ()));5     if(!request.getrequesturi (). Contains ("/baselogin/")){6       if(Stringutils.isblank (login_account) | | "NULL". Equalsignorecase (Login_account)) {7Response.sendredirect ("/baselogin/loginpage.htm");8         return false;9}Ten     } One     return true; A } -  -  Public voidPosthandle (httpservletrequest Request, httpservletresponse response, Object handler, Modelandview Modelandview)throwsException { theLogger.info ("--------posthandle-------" +Request.getrequesturi ()); - } -  -  Public voidAftercompletion (httpservletrequest Request, httpservletresponse response, Object handler, Exception ex) throwsException { +Logger.info ("--------aftercompletion-------" +Request.getrequesturi ()); - } +  A  Public voidafterconcurrenthandlingstarted (httpservletrequest Request, httpservletresponse response, Object handler) throwsException {Logger.info ("--------afterconcurrenthandlingstarted-------" +Request.getrequesturi ()); -}

Execution order: Prehandle, Controller, Posthandle, aftercompletion

Configuration:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/ Spring-mvc-3.2.xsd ">
<!--intercept exclusions
<mvc:interceptors>
        <mvc:interceptor>
<mvc:exclude-mapping path= ""/>
<bean class= "Org.bighead.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

-
<!--blocking Login-<mvc:interceptors>        <Mvc:interceptor>            <mvc:mappingPath= "/*/*.htm"/>            <Beanclass= "Org.bighead.interceptor.LoginInterceptor" />        </Mvc:interceptor>    </mvc:interceptors><Beans>

Introduction to Spring Interceptor (Interceptor)

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.