spring攔截器(interceptor)簡介


1. 攔截器用途

  (1)攔截未登錄用戶直接訪問某些鏈接

  (2)攔截日志信息

  (3)攔截非法攻擊,比如sql注入

2. 涉及jar、類

  (1)spring-webmvc.jar

  (2)HandlerInterceptor(org.springframework.web.servlet:接口)、

      AsyncHandlerInterceptor(org.springframework.web.servlet:接口)、 

      HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter:抽象類)

3.業務類

  (1)實現(implements) 實現HandlerInterceptor接口或者子接口

  (2)繼承(extends) 繼承HandlerInterceptor接口子類(抽象類)

  (3)涉及的方法

      preHandle、postHandle、afterCompletion

4.測試代碼

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 }

執行順序:preHandle -> controller -> postHandle -> afterCompletion

配置:

<?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
    ">
<!-- 攔截排除
<mvc:interceptors>
        <mvc:interceptor>
      <mvc:exclude-mapping path=""/>
      <bean class="org.bighead.interceptor.LoginInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

-->
<!-- 攔截登錄 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*/*.htm"/>
            <bean class="org.bighead.interceptor.LoginInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
<beans>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM