Spring interceptor (interceptor) Introduction, springinterceptor
1. interceptor Purpose
(1) intercept Unlogged users to directly access some links
(2) Intercepted log information
(3) Blocking illegal attacks, such as SQL Injection
2. jar and 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
(1) Implement the HandlerInterceptor interface or sub-interface
(2) Inheritance (extends) inherits the sub-class of the HandlerInterceptor interface (abstract class)
(3) methods involved
PreHandle, postHandle, afterCompletion
4. Test code
LoginInterceptor
1 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler throws Exception { 2 LOGGER.info("--------preHandle-------" + request.getRequestURI()); 3 HttpSession session = request.getSession(); 4 String 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)) { 7 response.sendRedirect("/baselogin/loginPage.htm"); 8 return false; 9 }10 }11 return true;12 }13 14 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception {15 LOGGER.info("--------postHandle-------" + request.getRequestURI());16 }17 18 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {19 LOGGER.info("--------afterCompletion-------" + request.getRequestURI());20 }21 22 public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {23 LOGGER.info("--------afterConcurrentHandlingStarted-------" + request.getRequestURI());24 }
Execution sequence: preHandle-> controller-> postHandle-> afterCompletion
Configuration:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "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 and exclude
<Mvc: interceptors>
<mvc:interceptor>
<mvc:exclude-mapping path=""/>
<bean class="org.bighead.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
-->
<! -- Interceptor --> <mvc: interceptors> <mvc: interceptor> <mvc: mapping path = "/*/*. htm "/> <bean class =" org. bighead. interceptor. loginInterceptor "/> </mvc: interceptor> </mvc: interceptors> <beans>