Ajax 请求session过期的统一处理
public class LoginInterceptor extends HandlerInterceptorAdapter { @SuppressWarnings("unused") private static final Logger log = Logger.getLogger(LoginInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // log.info("URL="+request.getRequestURL().toString()); Object obj = request.getSession().getAttribute(SecurityConstants.LOGIN_USER); if (null == obj) { if (WebUtil.isAjaxRequest(request)){ response.setHeader("sessionstatus", "timeout"); }else{ response.sendRedirect(request.getContextPath()+"/usr/login"); } return false; } return super.preHandle(request, response, handler); } }
public static boolean isAjaxRequest(HttpServletRequest request){ boolean result = false; String headerX = request.getHeader("X-Requested-With"); return headerX != null && headerX.equalsIgnoreCase("XMLHttpRequest"); }
/** * 设置未来(全局)的AJAX请求默认选项 * 主要设置了AJAX请求遇到Session过期的情况 */ $.ajaxSetup({ type: 'POST', complete: function(xhr,status) { var sessionStatus = xhr.getResponseHeader('sessionstatus'); if(sessionStatus == 'timeout') { var top = getTopWinow(); var yes = confirm('由于您长时间没有操作, session已过期, 请重新登录.'); if (yes) { top.location.href = '/skynk/index.html'; } } } }); /** * 在页面中任何嵌套层次的窗口中获取顶层窗口 * @return 当前页面的顶层窗口对象 */ function getTopWinow(){ var p = window; while(p != p.parent){ p = p.parent; } return p; }