1、web.xml 添加配置:

  1. <!-- session超时 -->
  2. <filter>
  3. <filter-name>sessionFilter</filter-name>
  4. <filter-class>com.dayhro.platform.filter.SessionTimeoutFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>sessionFilter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

2、sessionfilter.properties

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

3、SessionTimeoutFilter:

  1. import java.io.IOException;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Properties;
  5.  
  6. import javax.servlet.Filter;
  7. import javax.servlet.FilterChain;
  8. import javax.servlet.FilterConfig;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.ServletRequest;
  11. import javax.servlet.ServletResponse;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.servlet.http.HttpSession;
  15.  
  16. import org.apache.commons.lang.StringUtils;
  17.  
  18. import com.dayhr.web.util.PropertiesCommonUtil;
  19. import com.dayhr.web.util.StringUtil;
  20.  
  21. /**
  22. *
  23. * @ClassName:SessionTimeoutFilter
  24. * @Description: session超时处理
  25. * @author:
  26. * @date:2016年9月19日 下午7:59:25
  27. * @version
  28. */
  29. public class SessionTimeoutFilter implements Filter {
  30.  
  31. private Map<String, String> map = new HashMap<String, String>();
  32. private Map<String, String> suffixmap = new HashMap<String, String>();
  33. private static String loginUrl;
  34.  
  35. static{
  36. loginUrl = PropertiesCommonUtil.getValue("/properties/orgServer.properties", "login.url");
  37. }
  38.  
  39. @Override
  40. public void destroy() {
  41.  
  42. }
  43.  
  44. /**
  45. * 监听
  46. */
  47. @Override
  48. public void doFilter(ServletRequest request, ServletResponse response,
  49. FilterChain chain) throws IOException, ServletException {
  50.  
  51. HttpServletRequest httpRequest = (HttpServletRequest)request;
  52. HttpServletResponse httpResponse = (HttpServletResponse)response;
  53.  
  54. String contextPath=httpRequest.getContextPath();
  55. String requestUrl=httpRequest.getRequestURI().replace(contextPath, "");
  56.  
  57. if(requestUrl.indexOf(";")>-1){
  58. requestUrl = requestUrl.substring(0, requestUrl.indexOf(";")); //获取分号之前的字符串
  59. }
  60.  
  61. //当遇到不须过滤的直接跳过
  62. if(/*"/".equals(requestUrl)||*/requestUrl.contains("//")
  63. || map.get(StringUtil.parseSuffix(requestUrl,"url"))!=null
  64. || suffixmap.get(StringUtil.parseSuffix(requestUrl,"suffix"))!=null)
  65. {
  66. //可以跳过
  67. chain.doFilter(request, response);
  68. return ;
  69. }
  70.  
  71. HttpSession httpSession = httpRequest.getSession();
  72. if(httpSession == null || httpSession.getAttribute("userInfo") == null){
  73. //String userAgent = httpRequest.getHeader("User-Agent");
  74. String ajax = httpRequest.getHeader("X-Requested-With"); //XMLHttpRequest为ajax请求
  75.  
  76. if(StringUtils.isNotBlank(ajax)){ // ajax请求
  77. httpResponse.setHeader("sessionstatus", "timeout");
  78. //httpRequest.getRequestDispatcher("/user/sessionTimeoutWeb").forward(httpRequest, httpResponse);
  79. } else {
  80. if("/index.jsp".equals(requestUrl)){
  81. httpRequest.getRequestDispatcher("/index.jsp").forward(httpRequest, httpResponse);
  82. }else{
  83. httpResponse.sendRedirect(loginUrl+"/logout?source=dayHRO");
  84. }
  85. }
  86. }else{
  87. chain.doFilter(httpRequest, httpResponse);
  88. }
  89. }
  90.  
  91. /**
  92. * 初始化操作
  93. */
  94. @Override
  95. public void init(FilterConfig filterConfig) throws ServletException {
  96.  
  97. //获取过滤不用进行拦截的URL
  98. Properties properties = PropertiesCommonUtil.readPropertiesFile("/properties/sessionfilter.properties");
  99. String allowUrls = properties.getProperty("allowUrls");
  100. String suffixs = properties.getProperty("suffix");
  101.  
  102. if (allowUrls != null) {
  103. String[] st = allowUrls.split(",");
  104. map.clear();
  105. for (String s : st) {
  106. map.put(s, s);
  107. }
  108. }
  109. if (suffixs != null) {
  110. String[] str = suffixs.split(",");
  111. suffixmap.clear();
  112. for (String s : str) {
  113. suffixmap.put(s, s);
  114. }
  115. }
  116. }
  117.  
  118. }

4、jsp页面:

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

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. poj1082-Calendar Game-博弈/sg

    sg大法好 无脑sg即可,不用去找规律了. /*---------------------------------------------------------------------------- ...

  2. 学习笔记——Maven 内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  3. C#异步编程一

    前几天把Code First系列总结完,想着下步总结什么,原本想着XML,不过XML的内容比较多,还有3天班就中秋节了,想在中秋节前在完成一个系列,所以决定把异步这块总结下.说起异步可能会认为就是多线 ...

  4. 一个HTML5老兵坦言:我们真的需要“小程序”么?

    在PC时代,浏览器成为互联网信息的入口,并非因为它支持了HTML技术,而是因为它给人类带来了“世界是平的”的空间和理念,人类历史上第一次实现了信息的互联互通. 今天,微信虽然用了HTML5技术来做应用 ...

  5. js事件代理

    需要注意的blog:http://blog.csdn.net/majian_1987/article/details/8591385 一篇博客看懂  http://blog.csdn.net/maji ...

  6. AngularJS开发指南1:AngularJS简介

    什么是 AngularJS? AngularJS 是一个为动态WEB应用设计的结构框架.它能让你使用HTML作为模板语言,通过扩展HTML的语法,让你能更清楚.简洁地构建你的应用组件.它的创新点在于, ...

  7. 常用的Java 架包(jar)的用途

    前言:如果需要在项目中引入jar包,可以采用maven,配置方式在 http://mvnrepository.com 查询  slf4j-api 简介:slf4j并不是一种具体的日志系统,而是一个用户 ...

  8. 【CodeForces 604B】F - 一般水的题1-More Cowbe

    Description Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter ...

  9. [NOIP2010] 提高组 洛谷P1514 引水入城

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...

  10. 压力测试工具tsung

    tsung是用erlang开发的一款简单易用的压力测试工具,可以生成成千上万的用户模拟对服务器进行访问.目前对tsung的理解也仅限于会简单的应用,其内部结构没有深入研究过. 1.安装 tsung是用 ...