JS实现页面超时后自动跳转到登陆页面
网友方案:
var myTime = setTimeout("Timeout()", 60000);
function resetTime() {
clearTimeout(myTime);
myTime = setTimeout('Timeout()', 60000);
}
function Timeout() {
alert("您的登录已超时, 请点确定后重新登录!");
document.location.href='login.jsp';
}
document.documentElement.οnkeydοwn=resetTime;
document.doocumentElement.οnclick=resetTime;
实际操作:
window.onload = function caoke(){
setTimeout(wo,1000*10);
}
function wo(){
alert("由于您长时间没有操作,为保证您的安全,
请重新登录!");
window.location.href = "${ctx}" ;
if(window.parent != window){
window.parent.location.reload(true);
}
}
java 后台 过滤器实现
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
boolean isExclude = false ;
HttpServletRequest request = (HttpServletRequest) req ;
HttpServletResponse response = (HttpServletResponse) res ;
String url = request.getServletPath() ;
System.out.println(url);
for (String page : excludedPageArr) {//判断是否在过滤url之外
if(request.getServletPath().equals(page)){
isExclude = true;
break ;
}
}
String uString = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath() ;
if(isExclude){//在过滤url之外
chain.doFilter(request, response);
} else {//不在过滤url之外,判断session是否存在
HttpSession session = request.getSession();
int time = session.getMaxInactiveInterval();
if (session.getAttribute("user") == null) {
//这里进行了判断是不是ajax请求,ajax请求不能够把返回的js信息执行
if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
response.addHeader("sessionstatus", "timeOut");
response.addHeader("loginPath", request.getContextPath());
chain.doFilter(request, response);// ajax请求的判断,先放行,没有找到解决方案
} else {
//正常的请求可以直接弹出提示。
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
String data = "
data += "alert('由于您长时间没有操作,为保证您的安全,请重新登录!');" ;
data += "window.top.location.href = '"+request.getContextPath()+"' ;" ;
//data += "if(window.parent != window){" ;
//data += "window.parent.location.reload(true);" ;
//data += "}" ;
data += "" ;
writer.print(data);
writer.close();
}
} else {
chain.doFilter(request, response);
}
}
}
客户端JS,用于ajax请求session超时
对于jquery
$(document).ajaxComplete(function(event, xhr, settings) {
if(xhr.getResponseHeader("sessionstatus")=="timeOut"){
if(xhr.getResponseHeader("loginPath")){
alert("由于您长时间没有操作,为保证您的安全,请重新登录!");
window.top.location.href = xhr.getResponseHeader("loginPath") ;
}else{
alert("请求超时请重新登陆 !");
}
}
});
对于extjs的ajax请求
Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);
function checkUserSessionStatus(conn,response,options){
if(response.getResponseHeader("sessionstatus") == 'timeout'){
if(response.getResponseHeader("loginPath")){
alert("会话过期,请重新登陆!");
window.top.location.href = response.getResponseHeader("loginPath");
}else{
alert("请求超时请重新登陆 !");
}
}
}
如果使某个ajax请求不受全局方法的影响,那么可以在使用$.ajax()方法时,将参数中的global设置为false,jquery代码如下:
$.ajax({
url:"test.html",
global:false//不触发全局ajax事件
})