分享知識-快樂自己:SpringMVC 結合使用攔截器(判斷是否用戶是否已登陸)


基礎攔截器操作:

  攔截器是一種AOP操作實現,那么在AOP之中用戶一定不需要去關注攔截器的存在,用戶只需要按照自己已經習慣的處理方式進行代碼的編寫即可。

首先我們先創建一個自定義的攔截器:

package integration.uitl; import integration.bean.MlqUser; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SysInterceptor extends HandlerInterceptorAdapter { /** * 進入攔截器后首先進入的方法 * 返回false則不再繼續執行 * 返回true則繼續執行 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception { System.out.println("我是攔截器:我證明我進來了"); HttpSession session=request.getSession(); MlqUser userInfo = (MlqUser)session.getAttribute("UserInfo"); if(userInfo==null) { System.out.println("我證明用戶沒有登錄"); response.sendRedirect(request.getContextPath()+"/401.jsp"); return false; } System.out.println("我證明用戶已經登錄"); return  true; } /** * 生成視圖時執行,可以用來處理異常,並記錄在日志中 */ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception exception){ //-----------------//  } /** - * 生成視圖之前執行,可以修改ModelAndView */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3) throws Exception{ //----------------------------//  } }

接下來我們來配置核心文件:CustomInterceptor.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/aop
                       http://www.springframework.org/schema/aop/spring-aop.xsd
                       http://www.springframework.org/schema/tx
                       http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <!--自定義攔截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/sys/**"/>
            <bean class="integration.uitl.SysInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

下面我們來說明一下: "/sys/**" 的意思:就是說我要攔截所有以 /sys 開頭的訪問路徑,因為我Controller層 是這樣編寫的(可根據自己需求更改)

(加載時機:當我們直接訪問 /sys/main 的時候,會先走我們自定義的攔截器。此時從Session中獲取用戶對象信息。若是有信息則是已經登陸了,直接返回 true 通過。否則就 重新跳轉到登陸頁面,返回 False)

 

Face your past without regret. Handle your present with confidence.Prepare for future without fear. keep the faith and drop the fear.

面對過去無怨無悔,把握現在充滿信心,備戰未來無所畏懼。保持信念,克服恐懼!一點一滴的積累,一點一滴的沉淀,學技術需要不斷的積淀!


免責聲明!

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



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