1. 在web.xml中添加拦截器,对所有的*.do请求进行过滤
<!-- 登录过滤器 -->
<filter>
<filter-name>sessionTimeOutFilter</filter-name>
<filter-class>com.psdb.core.base.filter. SessionTimeOutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> sessionTimeOutFilter </filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
注:在web.xml中sessionTimeOutFilter必须放到shiroFilter的后面否则在sessionTimeOutFilter中SecurityUtils.getSubject().isAuthenticated()永远都是false。
2. 实现拦截器
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException{
// TODO Auto-generatedmethod stub
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
Subject currentUser = SecurityUtils.getSubject();
if(!currentUser.isAuthenticated()) {//判断session是否有效
//判断是否为ajax请求
if (request.getHeader("x-requested-with") !=null&&request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) {
//设置session状态
response.setHeader("session-status","timeout");
arg2.doFilter(request, response);
return;
}
}
arg2.doFilter(request, response);
}
3. 重写Extjs的Ext.data.proxy.Ajax类,在操作store时将store的proxy属性的type设置为‘psdbajax‘
Ext.define('psdbWeb.base.control.data.PsdbProxyAjax', {
extend: 'Ext.data.proxy.Ajax',
alias: 'proxy.psdbajax',
/**
* 重写Ext.data.proxy.Ajax的processResponse方法
* 新增当session过期时直接跳转到登录页面
*/
processResponse: function(success,operation, request, response) {
var me =this,
exception, reader, resultSet, meta;
if (me.destroying || me.destroyed) {
return;
}
me.fireEvent('beginprocessresponse', me, response,operation);
if (success ===true) {
reader = me.getReader();
if (response.status === 204) {
resultSet =reader.getNullResultSet();
} else {
resultSet =reader.read(me.extractResponseData(response), {
// If we're doing an update, we want to construct the models ourselves.
recordCreator:operation.getRecordCreator()
});
}
operation.process(resultSet,request, response);
exception =!operation.wasSuccessful();
} else {
me.setException(operation,response);
exception = true;
}
if (exception) {
//******************************************
Varsessionstatus=response.getResponseHeader("session-status");
if(sessionstatus=="timeout"){
//如果超时就处理,指定要跳转的页面
window.location.href=basePath+"/" ;
//********************************************
}else{
me.fireEvent('exception', me, response, operation);
}
}
// If a JsonReader detected metadata, process it now.
// This will fire the 'metachange' event which the Store processesto fire its own 'metachange'
else {
meta = resultSet.getMetadata();
if (meta) {
me.onMetaChange(meta);
}
}
me.afterRequest(request, success);
// Tell owning store processing has finished.
// It will fire its endupdate event which will cause interestedviews to
// resume layouts.
me.fireEvent('endprocessresponse', me, response,operation);
}
});
在store中使用
var store =Ext.create('Ext.data.Store', {
model: 'psdbWeb.page.topo.model.TopoStation',
proxy: {
type: 'psdbajax,
url: basePath+'topo/graph/topo/getStationList.do',
reader: {
type: 'json',
rootProperty: 'datas'
}
}
});4. 封装统一的ajax请求
/**
* 通用函数,封装了Ext.Ajax.request
* 对Ext.Ajax.request中的failure函数进行了重写,当后台出现错误时,弹出相应的提示信息
* 使用方法和Ext.Ajax.request(param)相同。
*/
request : function(param) {
var successFunction=param.success;
if(successFunction){
delete param.success;
param.success=function(response, options){
PsdbUtil.requestSuccessFunction(response,options,successFunction);
};
}
Ext.applyIf(param, {
timeout : 600000,
failure : function(response, options){
PsdbUtil.requestFailureFunction(response, options);
}
});
Ext.Ajax.mon(Ext.Ajax,'requestcomplete',function(conn ,response , options , eOpts){
var sessionstatus=response.getResponseHeader("session-status");
if(sessionstatus=="timeout"){
//如果超时就处理,指定要跳转的页面
window.location.href=basePath+"/" ;
}
});
Ext.Ajax.request(param);
}