首頁  >  文章  >  Java  >  Spring+SpringMVC+MyBatis深入學習及建構(十七)-SpringMVC攔截器

Spring+SpringMVC+MyBatis深入學習及建構(十七)-SpringMVC攔截器

PHP中文网
PHP中文网原創
2017-07-03 17:30:261443瀏覽

轉載請註明出處: 

#前面講到:Spring+SpringMVC+MyBatis深入學習及搭建(十六)-SpringMVC註解開發(高階篇)

1.攔截器定義

Spring Web MVC的處理器攔截器類似於Servlet開發中的過濾器Filter,用於對處理器進行預處理和後處理。

定義攔截器,實作HandlerInterceptor介面。接口中提供三個方法。

<span style="color: #0000ff">package</span><span style="color: #000000"> joanna.yan.ssm.interceptor;

</span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletRequest;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpServletResponse;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.servlet.HandlerInterceptor;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.servlet.ModelAndView;

</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> HandlerInterceptor1 <span style="color: #0000ff">implements</span><span style="color: #000000"> HandlerInterceptor{

    </span><span style="color: #008000">//</span><span style="color: #008000">执行Handler完成执行此方法
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景:统一异常处理,统一日志处理</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......afterCompletion"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之后,返回modelAndView之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景从modelAndView出发:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里同意指定视图</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......postHandle"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">用于身份认证、身份授权
    </span><span style="color: #008000">//</span><span style="color: #008000">比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行。</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......preHandle"<span style="color: #000000">);
        </span><span style="color: #008000">//</span><span style="color: #008000">return false表示拦截,不向下执行
        </span><span style="color: #008000">//</span><span style="color: #008000">return true表示放行</span>
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
    }

}</span>

2.攔截器設定

struts中是有一個大的攔截器鏈,它是一個共用的東西,可以把它加到任何的action鏈接,都讓它攔截。但是spring的攔截器不是全域的。

2.1針對某種mapping配置攔截器

springmvc攔截器針對HandlerMapping進行攔截設置,如果在某個HandlerMapping中設置攔截,經過該HandlerMapping映射成功的handler最終使用該攔截器。

<span style="color: #0000ff"><</span><span style="color: #800000">bean
    </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">property </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="interceptors"</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"><</span><span style="color: #800000">list</span><span style="color: #0000ff">></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">ref </span><span style="color: #ff0000">bean</span><span style="color: #0000ff">="handlerInterceptor1"</span><span style="color: #0000ff">/></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">ref </span><span style="color: #ff0000">bean</span><span style="color: #0000ff">="handlerInterceptor2"</span><span style="color: #0000ff">/></span>
        <span style="color: #0000ff"></</span><span style="color: #800000">list</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span>
<span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="handlerInterceptor1"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor1"</span><span style="color: #0000ff">/></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">id</span><span style="color: #0000ff">="handlerInterceptor2"</span><span style="color: #ff0000"> class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor2"</span><span style="color: #0000ff">/></span>

一般不建議使用。

2.2針對所有mapping配置全域攔截器

springmvc可以配置類似全域的攔截器,springmvc框架將配置的類似全域的攔截器注入到每個HandlerMapping中。

    <span style="color: #008000"><!--</span><span style="color: #008000">拦截器 </span><span style="color: #008000">--></span>
    <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptors</span><span style="color: #0000ff">></span>
        <span style="color: #008000"><!--</span><span style="color: #008000">多个拦截器,顺序执行 </span><span style="color: #008000">--></span>
        <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
            <span style="color: #008000"><!--</span><span style="color: #008000"> /**表示所有url包括子url路径 </span><span style="color: #008000">--></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">mvc:mapping </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="/**"</span><span style="color: #0000ff">/></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor1"</span><span style="color: #0000ff">></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"><</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">mvc:mapping </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="/**"</span><span style="color: #0000ff">/></span>
            <span style="color: #0000ff"><</span><span style="color: #800000">bean </span><span style="color: #ff0000">class</span><span style="color: #0000ff">="joanna.yan.ssm.interceptor.HandlerInterceptor2"</span><span style="color: #0000ff">></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span>
        <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptor</span><span style="color: #0000ff">></span>
    <span style="color: #0000ff"></</span><span style="color: #800000">mvc:interceptors</span><span style="color: #0000ff">></span>

3.攔截測試

3.1測試需求

測試多個攔截器各個方法的執行時機。

3.2寫兩個攔截器

 

3.3兩個攔截器都放行

運行日誌資訊:

<span style="color: #000000">HandlerInterceptor1...preHandle
HandlerInterceptor2...preHandle

HandlerInterceptor2...postHandle
HandlerInterceptor1...postHandle

HandlerInterceptor2...afterCompletion
HandlerInterceptor1...afterCompletion</span>

總結:

preHandle方法依序執行,postHandle和afterCompletion依攔截器配置的逆向順序執行。

3.4攔截器1放行,攔截器2不放行

運行日誌資訊:

<span style="color: #000000">HandlerInterceptor1...preHandle
HandlerInterceptor2...preHandle
HandlerInterceptor1...afterCompletion</span>

總結:

攔截器1放行,攔截器2的preHandle才會執行。

攔截器2的preHandle不放行,攔截器2的postHandle和afterCompletion不會執行。

只要有一個攔截器不放行,postHandle就不會執行。

3.5攔截器1不放行,攔截器2不放行

運行日誌資訊:

HandlerInterceptor1...preHandle

攔截器1的preHandle不放行,postHandle和afterCompletion不會執行。

攔截器1的preHandle不放行,攔截器2不執行。

4.小結

根據測試結果,對攔截器套用。

例如:統一日誌處理攔截器,需要改攔截器preHandle一定要放行,且將它放在攔截器鏈中的第一位置。

例如:登入認證攔截器,放在攔截器鏈中第一個位置。權限校驗攔截器,放在登入攔截器之後。 (因為登入通過後才校驗權限)

5.攔截器應用程式(實作登入認證)

5.1需求

(1)用戶請求url

(2)攔截器進行攔截校驗

  如果請求的url是公開位址(無需登入即可存取的url),讓放行

  如果使用者session不存在,跳到登入頁面。

  如果使用者session存在,放行,繼續操作。

5.2登入、登出controller方法

<span style="color: #0000ff">package</span><span style="color: #000000"> joanna.yan.ssm.controller;

</span><span style="color: #0000ff">import</span><span style="color: #000000"> javax.servlet.http.HttpSession;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.stereotype.Controller;
</span><span style="color: #0000ff">import</span><span style="color: #000000"> org.springframework.web.bind.annotation.RequestMapping;

@Controller
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span><span style="color: #000000"> LoginController {
    
    </span><span style="color: #008000">//</span><span style="color: #008000">登录</span>
    @RequestMapping("/login"<span style="color: #000000">)
    </span><span style="color: #0000ff">public</span> String login(HttpSession session, String username, String password) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{
        </span><span style="color: #008000">//</span><span style="color: #008000">调用service进行用户身份认证
        </span><span style="color: #008000">//</span><span style="color: #008000">...
        
        </span><span style="color: #008000">//</span><span style="color: #008000">在session中保存用户身份信息</span>
        session.setAttribute("username"<span style="color: #000000">, username);
        </span><span style="color: #0000ff">return</span> "redirect:items/queryItems.action"<span style="color: #000000">;
    }
    
    </span><span style="color: #008000">//</span><span style="color: #008000">退出</span>
    @RequestMapping("/logout"<span style="color: #000000">)
    </span><span style="color: #0000ff">public</span> String logout(HttpSession session) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{
        </span><span style="color: #008000">//</span><span style="color: #008000">清除session</span>
<span style="color: #000000">        session.invalidate();
        </span><span style="color: #0000ff">return</span> "redirect:items/queryItems.action"<span style="color: #000000">;
    }
    
}</span>

5.3登入認證攔截實作

5.3.1LoginInterceptor

#
<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> LoginInterceptor <span style="color: #0000ff">implements</span><span style="color: #000000"> HandlerInterceptor{

    </span><span style="color: #008000">//</span><span style="color: #008000">执行Handler完成执行此方法
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景:统一异常处理,统一日志处理</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......afterCompletion"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之后,返回modelAndView之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">应用场景从modelAndView出发:将公用的模型数据(比如菜单导航)在这里传到视图,也可以在这里同意指定视图</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> postHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, ModelAndView modelAndView) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......postHandle"<span style="color: #000000">);
    }

    </span><span style="color: #008000">//</span><span style="color: #008000">进入Handler方法之前执行
    </span><span style="color: #008000">//</span><span style="color: #008000">用于身份认证、身份授权
    </span><span style="color: #008000">//</span><span style="color: #008000">比如身份认证,如果认证不通过表示当前用户没有登录,需要此方法拦截不再向下执行。</span>
<span style="color: #000000">    @Override
    </span><span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span><span style="color: #000000"> preHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) </span><span style="color: #0000ff">throws</span><span style="color: #000000"> Exception {
        System.out.println(</span>"HandlerInterceptor1......preHandle"<span style="color: #000000">);
        </span><span style="color: #008000">//</span><span style="color: #008000">获取请求的url</span>
        String url=<span style="color: #000000">request.getRequestURI();
        </span><span style="color: #008000">//</span><span style="color: #008000">判断url是否是公开地址(实际使用时要将公开地址配置到文件中)
        </span><span style="color: #008000">//</span><span style="color: #008000">这里公开地址是登录提交的地址</span>
        <span style="color: #0000ff">if</span>(url.indexOf("login.action")>=0<span style="color: #000000">){
            </span><span style="color: #008000">//</span><span style="color: #008000">如果进行登录提交,放行</span>
            <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
        }
        </span><span style="color: #008000">//</span><span style="color: #008000">判断session</span>
        HttpSession session=<span style="color: #000000">request.getSession();
        String username</span>=(String) session.getAttribute("username"<span style="color: #000000">);
        </span><span style="color: #0000ff">if</span>(username!=<span style="color: #0000ff">null</span><span style="color: #000000">){
            </span><span style="color: #008000">//</span><span style="color: #008000">身份存在,放行</span>
            <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span><span style="color: #000000">;
        }
        
        </span><span style="color: #008000">//</span><span style="color: #008000">执行到这里,表示用户身份需要认证,跳转登录页面</span>
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp"<span style="color: #000000">).forward(request, response);
        
        </span><span style="color: #008000">//</span><span style="color: #008000">return false表示拦截,不向下执行
        </span><span style="color: #008000">//</span><span style="color: #008000">return true表示放行</span>
        <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span><span style="color: #000000">;
    }

}</span>

5.3.2攔截器設定

classpath下springmvc.xml中配置:

#如果此文對您有幫助,微信打賞我一下~ 

## ###

以上是Spring+SpringMVC+MyBatis深入學習及建構(十七)-SpringMVC攔截器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn