导读
由于项目需要,Ajax不支持重定向请求,当session失效后,点击按钮(发生的ajax请求)不会重定向登录页面。
问题描述
Ajax不支持重定向请求,当session失效后,点击按钮(发生的ajax请求)不会重定向登录页面。但是针对form表单形式的提交,可以通过配置sessionFilter来重定向到登录页面。
方式一
SessionFilter配置
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import com.entity.User;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* @about session过滤器,处理登录拦截
*/
public class SessionFilter implements Filter {
private static final Logger logger = LoggerFactory.getLogger(SessionFilter.class);
/**
* ajax请求标志
*/
private static final String XMLHTTPREQUEST = "XMLHttpRequest";
Gson gson = new Gson();
FilterConfig config = null;
//定义需要忽略的请求
private String[] ignoreArr = null;
@Override
public void destroy() {
this.config = null;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
boolean flag = isIgonre(request);
//需要忽略的请求
if (flag) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
//登录页面请求
String loginUrl = config.getInitParameter("loginAction");
//获取真实请求地址
GamsConfig.getConfig().contextPath = request.getContextPath();
//如果是登录页或者自定义过滤的请求,放行
if (requestUri.endsWith(loginStr)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
//从session里面获取用户信息
User user = (User) request.getSession().getAttribute("user");
// 请求是否是XMLHttpRequest类型
String type = request.getHeader("X-Requested-With") == null ? "" : request.getHeader("X-Requested-With");
if (user == null) {
if (StringUtils.equals(XMLHTTPREQUEST, type)) {
//告诉ajax这是重定向
response.setHeader("REDIRECT", "REDIRECT");
} else {
response.setHeader("SESSIONSTATUS", "TIMEOUT");
response.sendRedirect(loginUrl);
}
//重定向登录页面地址
response.setHeader("CONTEXTPATH", loginUrl);
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
} else {
filterChain.doFilter(servletRequest, servletResponse);
}
}
}
/**
* Tomcat启动时调用,进行初始化
*/
@Override
public void init(FilterConfig config) throws ServletException {
ignoreArr = config.getInitParameter("ignorePattern").split(",");
this.config = config;
//initWebSocket();
}
/**
* 忽略拦截的部分
*/
public boolean isIgonre(HttpServletRequest request) {
String path = request.getRequestURI().toLowerCase();
for (String ignore : ignoreArr) {
if (path.contains(ignore)) {
return true;
}
}
return false;
}
}
前端通用JS设置
项目基础js(项目自定的,我这个项目是common.js)中引入该方法:
//解决Ajax请求时无法重定向的问题
$.ajaxSetup( {
//设置ajax请求结束后的执行动作
complete : function(XMLHttpRequest, textStatus) {
// 通过XMLHttpRequest取得响应头,REDIRECT
var redirect = XMLHttpRequest.getResponseHeader("REDIRECT");
//若HEADER中含有REDIRECT说明后端想重定向
if (redirect === "REDIRECT") {
var win = window;
while (win !== win.top){
win = win.top;
}
//将后端重定向的地址取出来,使用win.location.href去实现重定向的要求
win.location.href= XMLHttpRequest.getResponseHeader("CONTEXTPATH");
}
}
});
web.xml配置
<!--session-->
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>com.filter.SessionFilter</filter-class>
<init-param>
<param-name>loginAction</param-name>
<param-value>/heioky/login.action</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
END
搞定~