Spring Web MVC's processor interceptors are similar to the filter filters in servlet development for preprocessing and post-processing of processors.
Define the interceptor to implement the Handlerinterceptor interface. Three methods are available in an interface.
1 Public classHandlerInterceptor1ImplementsHandlerinterceptor {2 3 4 //execute before entering the handler method5 //for identity authentication, identity authorization6 //such as identity authentication, if the authentication by means that the current user does not log in, need this method to intercept no longer execution down7 @Override8 Public BooleanPrehandle (httpservletrequest request,9HttpServletResponse response, Object handler)throwsException {Ten One //return False indicates interception, not down execution A //return TRUE indicates release - return false; - } the - //after entering the handler method, return to Modelandview before executing - //The scenario starts with Modelandview: Upload common model data (such as menu navigation) to the view here, or you can unify the specified view here - @Override + Public voidPosthandle (httpservletrequest request, - httpservletresponse Response, Object handler, +Modelandview Modelandview)throwsException { A at - } - - //execute handler complete execution of this method - //Application Scenario: Unified exception handling, unified log processing - @Override in Public voidaftercompletion (httpservletrequest request, - httpservletresponse Response, Object handler, Exception ex) to throwsException { + - the } * $}
- Interceptor Configuration
1, for handlermapping configuration
SPRINGMVC interceptors are set for handlermapping , and if blocking is configured in a handlermapping , the handlermapping the mapping succeeds Handler The Interceptor is eventually used.
1<Bean2 class= "Org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" >3<property name= "Interceptors" >4<list>5<ref bean= "HandlerInterceptor1"/>6<ref bean= "HandlerInterceptor2"/>7</list>8</property>9</bean>Ten<bean id= "HandlerInterceptor1"class= "Springmvc.intercapter.HandlerInterceptor1"/> One<bean id= "HandlerInterceptor2"class= "Springmvc.intercapter.HandlerInterceptor2"/>
2, the global interceptor
1 <!--Interception Device -2 <mvc:interceptors>3 <!--multiple interceptors, sequential execution -4 <Mvc:interceptor>5 <mvc:mappingPath="/**"/>6 <Beanclass= "Com.luchao.springmvc.filter.HandlerInterceptor1"></Bean>7 </Mvc:interceptor>8 <Mvc:interceptor>9 <mvc:mappingPath="/**"/>Ten <Beanclass= "Com,luchao.springmvc.filter.handlerinterceptor2"></Bean> One </Mvc:interceptor> A </mvc:interceptors>
Write two interceptors to intercept the test.
1, Two interceptors are released .
1 Handlerinterceptor1...prehandle 2 Handlerinterceptor2...prehandle 3 4 Handlerinterceptor2...posthandle 5 Handlerinterceptor1...posthandle 6 7 handlerinterceptor2...aftercompletion 8 Handlerinterceptor1...aftercompletion
Summarize:
The Prehandle method executes sequentially,
Posthandle and aftercompletion are executed in reverse order of interceptor configuration.
2, Interceptor 1 release, Interceptor 2 not released
1 Handlerinterceptor1...prehandle 2 Handlerinterceptor2...prehandle 3 handlerinterceptor1...aftercompletion
Summarize:
Interceptor 1 release, Interceptor 2 prehandle will be executed.
Interceptors 2 Prehandle not released, interceptors 2 posthandle and aftercompletion will not be executed.
Posthandle will not execute as long as an interceptor is not released.
3, Interceptor 1 does not release, Interceptor 2 does not release
1 Handlerinterceptor1...prehandle
Interceptors 1 prehandle not released, Posthandle and Aftercompletion will not be executed.
Interceptor 1 prehandle not release, Interceptor 2 does not execute.
Summarize:
Based on the test results, the interceptor is applied. For example: Unified log processing Interceptor, need the Interceptor Prehandle must be released, and put it in the Interceptor link in the first position. For example: Login to the authentication interceptor, placed in the Interceptor link in the first position. The permission check blocker is placed after the login authentication interceptor. (check permissions only after login)
- Interceptor Application (for login authentication)
1. Controller method
@Controller Public classLogincontroller {//Login@RequestMapping ("/login") PublicString Login (HttpSession session, string Username, string password)throwsException {//invoking service for user authentication// ... //Save user identity information in sessionSession.setattribute ("username", username); //REDIRECT to Product List page return"Redirect:/items/queryitems.action"; } //Exit@RequestMapping ("/logout") PublicString Logout (HttpSession session)throwsException {//Clear Sessionsession.invalidate (); //REDIRECT to Product List page return"Redirect:/items/queryitems.action"; }}
2, Login Authentication Interceptor
1 Public classLogininterceptorImplementsHandlerinterceptor {2 3 4 //execute before entering the handler method5 //for identity authentication, identity authorization6 //such as identity authentication, if the authentication by means that the current user does not log in, need this method to intercept no longer execution down7 @Override8 Public BooleanPrehandle (httpservletrequest request,9HttpServletResponse response, Object handler)throwsException {Ten One //gets the requested URL AString URL =Request.getrequesturi (); - //determine if the URL is a public address (it will be exposed in the address configuration profile when it is actually used) - //Here the address is the address of the landing submission the if(Url.indexof ("Login.action") >=0){ - //If the login is submitted, the release - return true; - } + - //Judging Session +HttpSession session =request.getsession (); A //Remove user identity information from the session atString username = (string) session.getattribute ("username"); - - if(Username! =NULL){ - //identity exists, release - return true; - } in - //Execute here indicates user identity needs authentication, jump landing page toRequest.getrequestdispatcher ("/web-inf/jsp/login.jsp"). Forward (request, response); + - //return False indicates interception, not down execution the //return TRUE indicates release * return false; $}
SPRINGMVC Learning-Interceptors