當session失效,頁面跳轉到登陸介面的處理
每個系統頁面操作過程中都有一個session,session可以存放少量的使用者資訊,供我們的頁面操作使用。當session超時失效的時候我們就要重新往session中寫入登陸的使用者資訊,而這個寫入的操作一般寫在在使用者成功登陸系統的時候,所以當session失效時,我們頁面中所有的操作都要監聽,然後跳轉到登陸的介面重新登陸。
1、設定session有效時間
在web.xml裡面設定:
<!-- session失效時間 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
2、增加一個攔截器,攔截到後臺的請求
package com.ht.spring.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class SystemSessionInterceptor implements HandlerInterceptor{
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res,
Object arg2) throws Exception {
HttpSession session = req.getSession(true);
String userCode = (String) session.getAttribute("userCodeModel");
String path = req.getSession().getServletContext().getContextPath() + "/jsps/login.jsp";
/*if(userCode == null || "".equals(userCode)){
res.sendRedirect(path);
return false;
throw new SessionTimeoutException();
}else{
return true;
}*/
/*if(req.getHeader("x-requested-with")!=null
&&req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
res.setHeader("sessionStatus", "timeout");
}
res.getWriter().print("timeout");
return true;*/
if(userCode == null || "".equals(userCode)){
if(req.getHeader("x-requested-with")!=null
&&req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
res.setHeader("sessionStatus", "timeout");
}else{
res.sendRedirect(path);
}
return false;
}
return true;
}
}
3、在spring-mvc.xml裡面加這個攔截器的攔截過濾
<!-- session失效攔截 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" /><!-- 攔截的目錄-->
<mvc:exclude-mapping path="/user/login.do" /><!-- 不需要攔截的請求-->
<mvc:exclude-mapping path="/user/loginAgain.do" />
<mvc:exclude-mapping path="/jsps" />
<mvc:exclude-mapping path="/jquery-easyui-1.5.3" />
<mvc:exclude-mapping path="/content" />
<mvc:exclude-mapping path="/WEB-INF" />
<bean class="com.ht.spring.interceptor.SystemSessionInterceptor"></bean><!-- 攔截器的載入類-->
</mvc:interceptor>
</mvc:interceptors>
4、增加一個異常處理的js,在每一個頁面加以引用
$.ajaxSetup({
error:function(XMLHttpRequest, textStatus, errorThrown){
if(XMLHttpRequest.status==403){
alert("您沒有許可權訪問此資源或者進行操作");
return false;
}
},
complete:function(XMLHttpRequest, textStatus){
var sessionStatus = XMLHttpRequest.getResponseHeader("sessionStatus");
if(sessionStatus == "timeout"){
$.messager.alert("系統提示", "登入超時,請重新登入");
setTimeout(function(){
var top = getTopWinow();
//$.messager.alert("系統提示", "登入超時,請重新登入");
var curPath = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = curPath.indexOf(pathName);
var lacalhostPath = curPath.substring(0,pos);
var prjName = pathName.substring(0,pathName.substr(1).indexOf("/")+1);
//top.location.href="${pageContext.request.contextPath}"+"/jsps/login.jsp";
top.location.href = lacalhostPath + prjName +"/jsps/login.jsp";
},1000);
}
}
});
function getTopWinow(){
var p =window;
while(p != p.parent){
p = p.parent;
}
return p;
}