1、web.xml 添加配置:

    <!-- session超时 -->
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>com.dayhro.platform.filter.SessionTimeoutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2、sessionfilter.properties

#***********************************
# sessionouttimefilter配置
#***********************************
#判断以下的数据,拦截器直接跳过;以逗号分隔;
allowUrls=login.do,login.jsp,logout.do,404.html,500.html,getSmsCode.do,codeCallBack.do,error.jsp
#判断以下后缀名,也直接跳过;以逗号分隔;
suffix=js,css,jpg,jpeg,ico,png,zip,swf,xml,gif,ftl,php,apk,ipa,rar,mp3,wav,rmvb,doc,xls,ppt,woff,ttf
hippsuffix=/sso/to_hippo.jsp
#移动端请求放行
mobilesuffix=/mobile/
#WEBSERVICE请求放行
webservicesuffix=/webws/
#客户指引请求放行
guidancesuffix=/guidance/
#与外包会话保持线程,每十分钟一次
#baseUrl=http://localhost:80
chinese=\u4E2D\u6587

3、SessionTimeoutFilter:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import org.apache.commons.lang.StringUtils; import com.dayhr.web.util.PropertiesCommonUtil;
import com.dayhr.web.util.StringUtil; /**
*
* @ClassName:SessionTimeoutFilter
* @Description: session超时处理
* @author:
* @date:2016年9月19日 下午7:59:25
* @version
*/
public class SessionTimeoutFilter implements Filter { private Map<String, String> map = new HashMap<String, String>();
private Map<String, String> suffixmap = new HashMap<String, String>();
private static String loginUrl; static{
loginUrl = PropertiesCommonUtil.getValue("/properties/orgServer.properties", "login.url");
} @Override
public void destroy() { } /**
* 监听
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest)request;
HttpServletResponse httpResponse = (HttpServletResponse)response; String contextPath=httpRequest.getContextPath();
String requestUrl=httpRequest.getRequestURI().replace(contextPath, ""); if(requestUrl.indexOf(";")>-1){
requestUrl = requestUrl.substring(0, requestUrl.indexOf(";")); //获取分号之前的字符串
} //当遇到不须过滤的直接跳过
if(/*"/".equals(requestUrl)||*/requestUrl.contains("//")
|| map.get(StringUtil.parseSuffix(requestUrl,"url"))!=null
|| suffixmap.get(StringUtil.parseSuffix(requestUrl,"suffix"))!=null)
{
//可以跳过
chain.doFilter(request, response);
return ;
} HttpSession httpSession = httpRequest.getSession();
if(httpSession == null || httpSession.getAttribute("userInfo") == null){
//String userAgent = httpRequest.getHeader("User-Agent");
String ajax = httpRequest.getHeader("X-Requested-With"); //XMLHttpRequest为ajax请求 if(StringUtils.isNotBlank(ajax)){ // ajax请求
httpResponse.setHeader("sessionstatus", "timeout");
//httpRequest.getRequestDispatcher("/user/sessionTimeoutWeb").forward(httpRequest, httpResponse);
} else {
if("/index.jsp".equals(requestUrl)){
httpRequest.getRequestDispatcher("/index.jsp").forward(httpRequest, httpResponse);
}else{
httpResponse.sendRedirect(loginUrl+"/logout?source=dayHRO");
}
}
}else{
chain.doFilter(httpRequest, httpResponse);
}
} /**
* 初始化操作
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException { //获取过滤不用进行拦截的URL
Properties properties = PropertiesCommonUtil.readPropertiesFile("/properties/sessionfilter.properties");
String allowUrls = properties.getProperty("allowUrls");
String suffixs = properties.getProperty("suffix"); if (allowUrls != null) {
String[] st = allowUrls.split(",");
map.clear();
for (String s : st) {
map.put(s, s);
}
}
if (suffixs != null) {
String[] str = suffixs.split(",");
suffixmap.clear();
for (String s : str) {
suffixmap.put(s, s);
}
}
} }

4、jsp页面:

//session失效登出
$.ajaxSetup({
contentType: "application/x-www-form-urlencoded;charset=utf-8"
,complete: function (XMLHttpRequest, textStatus) {
var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus"); // 通过XMLHttpRequest取得响应头,sessionstatus,
if (sessionstatus == "timeout") {
// 如果超时就处理 ,指定要跳转的页面
window.location.href = "/dayhro-web/DayhroLogin/logout";
}
}
});

Session超时处理的更多相关文章

  1. Java设置session超时(失效)的三种方式

    1. 在web容器中设置(此处以tomcat为例) 在tomcat-6.0\conf\web.xml中设置,以下是tomcat 6.0中的默认配置: <!-- ================= ...

  2. session超时时间设置方法

    session超时时间设置方法 由于session值之前没有设置,以至于刚登录的网站,不到一分钟就超时了,总结了一下,原来是session过期的原因,以下是设置session时间的3个方法: 1. 在 ...

  3. ASP.NET多次点击提交按钮以及Session超时和丢失过期问题

    1.ASP.NET防止多次点击提交按钮 对于一个按钮,要让变成恢色的,只要this.disabled=true就可以了,可是在.NET里,添加了OnClick事件后,就无法提交信息了.所以要加上以下代 ...

  4. Spring mvc Interceptor 解决Session超时配置流程

    最近公司内部框架中对Session超时这一功能未实现,由于采用iframe结构,Session超时后,当点击左侧系统菜单时,会在iframe的右侧再次弹出登陆框. 该问题是由于没有设置拦截器造成. 添 ...

  5. c# webConfig中的session超时详细设置

    webConfig中的session超时详细设置 我们在webConfig中设置Session超时的时候,如果最后发行的地址是远程服务器,我们很多不是必须的属性并不用设置,如果设之后,倒不能让 ses ...

  6. 设置session超时

    在web应用中,设置session超时有三种方法: 1.在web.xml文件中配置:单位是分钟,范围是针对本项目所有用户的session <session-config> <sess ...

  7. spring security:ajax请求的session超时处理

    当前端在用ajax请求时,如果没有设置session超时时间并且做跳转到登录界面的处理,那么只是靠后台是很难完成超时的一系列动作的:但是如果后台 没有封装一个ajax请求公共类,那么在ajax请求上下 ...

  8. 配置SESSION超时与请求超时

    <!--项目的web.xml中 配置SESSION超时,单位是min.用户在线时间.如果不设置,tomcat下的web.xml的session-timeout为默认.--><sess ...

  9. Java设置session超时(失效)的时间

    在一般系统登录后,都会设置一个当前session失效的时间,以确保在用户长时间不与服务器交互,自动退出登录,销毁session具体设置的方法有三种:1.在web容器中设置(以tomcat为例)在tom ...

随机推荐

  1. Chrome扩展开发(Gmail附件管理助手)系列之〇——概述

    目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...

  2. HoloLens开发手记 - Unity之Persistence 场景保持

    Persistence 场景保持是HoloLens全息体验的一个关键特性,当用户离开原场景中时,原场景中全息对象会保持在特定位置,当用户回到原场景时,能够准确还原原场景的全息内容.WorldAncho ...

  3. mysql慢查询分析工作pt-query-digest的使用

    一.简单安装 wget percona.com/get/pt-query-digest chmoe u+x pt-query-digest 二.简单使用 ./pt-query-digest /var/ ...

  4. 云计算之路-阿里云上:2014年6月11日17点遇到的CPU 100%状况

    今天下午17:00-17:05之间,在请求量没有明显变化的情况下,SLB中的1台云服务器的CPU突然串到100%(当时SLB中一共有3台云服务器),见下图: 造成的直接后果是请求执行时间变得超长,最长 ...

  5. Java学习笔记(十八)——Java DTO

    [前面的话] 在和技术人员的交流中,各种专业术语会出现,每次都是默默的记录下出现的术语,然后再去网上查看是什么意思.最近做项目,需要使用到DTO,然后学习一下吧. 这篇文章是关于Java DTO的,选 ...

  6. Bootstrap3.0学习第二十六轮(JavaScript插件——图片轮播)

    详情请查看http://aehyok.com/Blog/Detail/32.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  7. Address already in use: JVM_Bind<null>:80

    Address already in use: JVM_Bind<null>:80 咱还是闲话少说,直接切入正题. 起因: 一直用Tomcat,但是前几天突然报错: java.net.Bi ...

  8. 基本的mediaQuery写法,不复习又忘记了

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)

    今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...

  10. 【poj1090】 Chain

    http://poj.org/problem?id=1090 (题目链接) 题意 给出九连环的初始状态,要求将环全部取下需要走多少步. Solution 格雷码:神犇博客 当然递推也可以做. 代码 / ...