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;
User user = (User) request.getSession().getAttribute("user");
String type = request.getHeader("X-Requested-With") == null ? "" : request.getHeader("X-Requested-With");
if (user == null) {
if (StringUtils.equals(XMLHTTPREQUEST, type)) {
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;
* 忽略拦截的部分
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)中引入该方法:
$.ajaxSetup( {
complete : function(XMLHttpRequest, textStatus) {
var redirect = XMLHttpRequest.getResponseHeader("REDIRECT");
if (redirect === "REDIRECT") {
var win = window;
while (win !== win.top){
win = win.top;
win.location.href= XMLHttpRequest.getResponseHeader("CONTEXTPATH");
web.xml配置
<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>
chaohen:www.yuque.com/heioky
搞定~