Session超时处理
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超时处理的更多相关文章
- Java设置session超时(失效)的三种方式
1. 在web容器中设置(此处以tomcat为例) 在tomcat-6.0\conf\web.xml中设置,以下是tomcat 6.0中的默认配置: <!-- ================= ...
- session超时时间设置方法
session超时时间设置方法 由于session值之前没有设置,以至于刚登录的网站,不到一分钟就超时了,总结了一下,原来是session过期的原因,以下是设置session时间的3个方法: 1. 在 ...
- ASP.NET多次点击提交按钮以及Session超时和丢失过期问题
1.ASP.NET防止多次点击提交按钮 对于一个按钮,要让变成恢色的,只要this.disabled=true就可以了,可是在.NET里,添加了OnClick事件后,就无法提交信息了.所以要加上以下代 ...
- Spring mvc Interceptor 解决Session超时配置流程
最近公司内部框架中对Session超时这一功能未实现,由于采用iframe结构,Session超时后,当点击左侧系统菜单时,会在iframe的右侧再次弹出登陆框. 该问题是由于没有设置拦截器造成. 添 ...
- c# webConfig中的session超时详细设置
webConfig中的session超时详细设置 我们在webConfig中设置Session超时的时候,如果最后发行的地址是远程服务器,我们很多不是必须的属性并不用设置,如果设之后,倒不能让 ses ...
- 设置session超时
在web应用中,设置session超时有三种方法: 1.在web.xml文件中配置:单位是分钟,范围是针对本项目所有用户的session <session-config> <sess ...
- spring security:ajax请求的session超时处理
当前端在用ajax请求时,如果没有设置session超时时间并且做跳转到登录界面的处理,那么只是靠后台是很难完成超时的一系列动作的:但是如果后台 没有封装一个ajax请求公共类,那么在ajax请求上下 ...
- 配置SESSION超时与请求超时
<!--项目的web.xml中 配置SESSION超时,单位是min.用户在线时间.如果不设置,tomcat下的web.xml的session-timeout为默认.--><sess ...
- Java设置session超时(失效)的时间
在一般系统登录后,都会设置一个当前session失效的时间,以确保在用户长时间不与服务器交互,自动退出登录,销毁session具体设置的方法有三种:1.在web容器中设置(以tomcat为例)在tom ...
随机推荐
- Django添加Last-Modified和ETag
用Django REST Framework做的REST API,其中有个API有这样的需求: APP端请求这个API,服务器端从数据库读数据,返回json.返回的数据量稍微有些大,但是可能一年才修改 ...
- Javascript设计模式之我见:迭代器模式
大家好!本文介绍迭代器模式及其在Javascript中的应用. 模式介绍 定义 提供一种方法顺序一个聚合对象中各个元素,而又不暴露该对象内部表示. 类图及说明 Iterator抽象迭代器 抽象迭代器负 ...
- HoloLens开发手记 - Unity之场景共享 Shared holographic experiences in Unity
佩戴HoloLens的多个用户可以使用场景共享特性来获取集合视野,并可以与固定在空间中某个位置的同一全息对象进行交互操作.这一切是通过空间锚共享(Anchor Sharing)来实现的. 为了使用共享 ...
- 如何自定义FloatingActionButton的大小
Google最近为了让开发者更好的更规范的应用Material Design设计思想,特意放出了android support design library,里面含有更多Material Design ...
- Quartz.Net在windows服务中的使用
写在前面 这几天在弄一个项目,需要定时抓取一些数据,当时也想直接用timer算了.因为之前也弄过这样的项目,但是一想,已经用过了,再去使用同一种思路,未免太乏味了.就换了一种新玩法.这里将之前看到的一 ...
- 每天一个linux命令(41):at命令
在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...
- OC基础--OC内存管理原则和简单实例
ARC: 由于自己的学习视频太早,Xcode是iOS6版本,新建命令行项目后,系统会默认启动ARC机制,全程Automatic Reference Counting,简单的说,就是代码中自动加入了re ...
- OC基础--self关键字&super关键字
PS:OC中的self关键字可以与C#中的this关键字区分记忆,虽然区别还是很大的. OC中的super关键字指的是父类指针 一.self关键字必须了解的知识: 1.使用场合:只能用在方法中(对象方 ...
- JQuery中的小技巧,,,连载中。。。
获取下拉框中选中项的文本等操作 jQuery获取Select元素,并选择的Text和Value: 1.获取select 选中的 text : $("#ddlRegType").f ...
- Mysql-日期转换
一.字符串转日期 下面将讲述如何在MYSQL中把一个字符串转换成日期: 背景:rq字段信息为:20100901 1.无需转换的: SELECT * FROM tairlist_day WHERE rq ...