1. 程式人生 > >springmvc攔截器 登入快取失效然後跳轉重新登入

springmvc攔截器 登入快取失效然後跳轉重新登入

1.新增配置檔案dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd             http://www.springframework.org/schema/context              http://www.springframework.org/schema/context/spring-context-3.0.xsd             http://www.springframework.org/schema/aop              http://www.springframework.org/schema/aop/spring-aop-3.0.xsd             http://www.springframework.org/schema/tx              http://www.springframework.org/schema/tx/spring-tx-3.0.xsd             http://www.springframework.org/schema/mvc              http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd             http://www.springframework.org/schema/context              http://www.springframework.org/schema/context/spring-context-3.0.xsd">     <!--         使Spring支援自動檢測元件,如註解的Controller     -->     <context:component-scan base-package="glxt.jzrj.web" />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">         <property name="prefix" value="/WEB-INF/projectManage/" />         <property name="suffix" value=".html" />     </bean>          <mvc:interceptors>         <mvc:interceptor>             <!--設定攔截的路徑  /** 代表攔截所有-->             <mvc:mapping path="/**" />             <!-- 設定不需要攔截的路徑  見下圖 1-1-->             <mvc:exclude-mapping path="/sysLogin/*" />             <bean class="glxt.jzrj.web.AuthFilterInterceptor"><!-- 在這個類裡面寫攔截的程式碼 -->                 <!--outsideOfficeHoursPage屬性指定頁面的URL-->                 <property name="outsideOfficeHoursPage">                     <value>/glxt/index.jsp                     </value>                 </property>             </bean>         </mvc:interceptor>     </mvc:interceptors>     <bean id="messageSource"         class="org.springframework.context.support.ResourceBundleMessageSource"         p:basename="message">     </bean> </beans>

圖1-1

2. 在application-context.xml引入新增的配置檔案(該檔案在web.xml裡引入所以只需要在application-context.xml配置檔案裡面引入攔截器的配置檔案就行,注意引入的路徑  我這裡設定的是classpath 根據個人專案而定)

3.新增攔截程式碼(新增一個類)程式碼如下

package glxt.jzrj.web;

import glxt.jzrj.units.CommonUtils;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthFilterInterceptor extends HandlerInterceptorAdapter {     // 繼承HandlerInterceptorAdapter類

    private String outsideOfficeHoursPage;// outsideOfficeHoursPage屬性指定錯誤提示頁面的URL

    // 重寫 preHandle()方法,在業務處理器處理請求之前對該請求進行攔截處理     public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {         String urlString = request.getRequestURI();         System.out.println(urlString);         String userId = request.getSession().getAttribute("userId")+"";         System.out.println(userId);         if(CommonUtils.isNotEmpty(userId) && !"null".equals(userId)){             return true;         } else {             response.sendRedirect(outsideOfficeHoursPage); // 返回提示頁面             return false;         }     }

    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object o, ModelAndView mav)             throws Exception {     }

    public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object o, Exception excptn)             throws Exception {     }

    public String getOutsideOfficeHoursPage() {         return outsideOfficeHoursPage;     }

    public void setOutsideOfficeHoursPage(String outsideOfficeHoursPage) {         this.outsideOfficeHoursPage = outsideOfficeHoursPage;     }

}

注意:路徑需要仔細檢視 以及 防止出錯,由於環境存在差異如有建議請留言評論

 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  只有3.2及以上才會有

<mvc:exclude-mapping path="/sysLogin/*" />這個標籤

根據個人需要調整版本