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)