轉載自網址:http://www.2cto.com/kf/201311/260303.html;
近期發現以前的系統中存在一個如下的Bug Case:
在Session過期時,執行頁面的ajax請求時,無法正常跳轉到session過期提示頁面,系統直接hold在那里不動,只有點擊左側菜單時,系統才能跳轉到session過期提示頁面。
經過調研,發現是攔截器的處理問題,攔截器只攔截了Http請求,而沒有攔截Ajax請求,才導致出現上述Bug。
下面是解決辦法:
首先,優化攔截器:
[java]
/**
* 攔截器
* @author lyh
* @version 2013-11-1
* @see LoginFilter
* @since
*/
public class LoginFilter implements Interceptor
{
/**
* 序列號
*/
private static final long serialVersionUID = -4979037503367919375L;
/**
* 日志
*/
private static final Logger LOG = Logger.getLogger(LoginFilter.class);
/**
* ajax session超時返回值
*/
private static String AJAX_TIME_OUT = null;
/**
* ajax 請求無權限返回值
*/
private static String AJAX_NO_LIMIT = null;
/**
* ajax 請求異常返回值 (在每個ajax請求中處理)
*/
//private static String AJAX_EXCEPTION = null;
/**
* 放行url
*/
private static List<String> awayUrls = null;
static
{
AJAX_TIME_OUT = "ajaxSessionTimeOut";
AJAX_NO_LIMIT = "ajaxNoLimit";
//AJAX_EXCEPTION = "ajaxException";
awayUrls = new LinkedList<String>();
//awayUrls.add("/login!userLogin.action");
//awayUrls.add("/custom!toLogin.action");
awayUrls.add("/equipment!upLoad.action");
}
@Override
public String intercept(ActionInvocation invocation)
throws Exception
{
//獲取request域中信息
HttpServletRequest req = ServletActionContext.getRequest();
//獲得當前請求url
String url = req.getServletPath();
//獲得請求類型
String type = req.getHeader("X-Requested-With");
//Object object = (Object)invocation.getAction();
//如果當前url在放行url集合內 則直接放行
if (!awayUrls.contains(url))
{
UserInfoBean userinfo = (UserInfoBean)req.getSession().getAttribute(
CommonConstant.AUTH_SESSION_USER_KEY);
if (userinfo == null)
{
LOG.debug("用戶登錄會話已過期!");
//ajax請求 session過期時 返回字符串
if ("XMLHttpRequest".equalsIgnoreCase(type))
{
PrintWriter printWriter = ServletActionContext.getResponse().getWriter();
printWriter.print(AJAX_TIME_OUT);
printWriter.flush();
printWriter.close();
return null;
}
//普通http請求 直接返回頁面
else
{
return "sessionTimeOut";
}
}
else
{
//鑒權結果
boolean authFlag = false;
try
{
//執行鑒權
authFlag = userManager_Service.isUrlInUserLimit(userinfo.getU_phone_num(),
url);
}
catch (Exception e)
{
LOG.error(" 鑒權出現異常!異常信息:" + e.toString() + ":" + e.getMessage());
}
//鑒權通過則放行 否則攔截
if (authFlag)
{
return invocation.invoke();
}
//鑒權不通過
else
{
//ajax請求 session過期時 返回字符串
if ("XMLHttpRequest".equalsIgnoreCase(type))
{
PrintWriter printWriter = ServletActionContext.getResponse().getWriter();
printWriter.print(AJAX_NO_LIMIT);
printWriter.flush();
printWriter.close();
return null;
}
//其他Http請求 直接返回頁面
else
{
return "noLimit";
}
}
}
}
else
{
return invocation.invoke();
}
}
@Override
public void destroy()
{
//do yourself
}
@Override
public void init()
{
//do yourself
}
}
上述攔截器考慮了Ajax和Http兩種情況,Http請求被攔截時,直接跳轉到指定的全局頁面,而Ajax請求被攔截時則采用Js方式提示用戶。
[html]
<package name="self-default" extends="json-default">
<interceptors>
<interceptor name="loginFilter" class="xx.xx.LoginFilter" />
<interceptor-stack name="mydefault">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="tokenSession">
<param name="includeMethods">add*,update*,modify*,upload*</param>
</interceptor-ref>
<interceptor-ref name="loginFilter" />
</interceptor-stack>
</interceptors>
<!-- 攔截器應用到全部action -->
<default-interceptor-ref name="mydefault"/>
<global-results>
<!-- 普通http請求時 系統出現異常返回到錯誤頁 -->
<result name="exception">/file/smartmanager/public/globalException.
jsp</result>
<!-- 普通http請求時 無操作權限 -->
<result name="noLimit">/file/smartmanager/public/noLimit.jsp</result>
<!-- 普通http請求時 session過期 -->
<result name="sessionTimeOut">/file/smartmanager/public/sessionTimeOut.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 全局異常返回exception字符串 -->
<exception-mapping exception="java.lang.Exception" result="exception" />
</global-exception-mappings>
</package>
下面是一個簡單的Action例子:
[java]
public class MyAction
{
/**
* Http請求
* @return
* @throws Exception
* @see
*/
public String httpReqMethod()
throws Exception
{
try
{
//do yourself
}
catch(Exception e)
{
//捕獲異常時拋出 觸發global-results中的exception 然后跳轉到提示頁面
throw e;
}
return "httpReqMethod";
}
/**
* Ajax請求
* @return
* @throws Exception
* @see
*/
public String ajaxReqMethod()
{
try
{
//do yourself
}
catch(Exception e)
{
//no throw
//此處在捕獲異常時 添加提示信息至json 方便在頁面展示
//ajaxMap.put("success", false);
//ajaxMap.put("opMsg", ResultMsg.CHANGE_PWD_ERROR);
}
return "ajaxReqMethod";
}
}
配置此Action的xml(此Action需要被攔截 故extends定義的攔截器self-default):
[html]
<package name="willPackage" extends="self-default" namespace="/">