导读


由于项目需要,Ajax不支持重定向请求,当session失效后,点击按钮(发生的ajax请求)不会重定向登录页面。

问题描述


Ajax不支持重定向请求,当session失效后,点击按钮(发生的ajax请求)不会重定向登录页面。但是针对form表单形式的提交,可以通过配置sessionFilter来重定向到登录页面。

方式一

SessionFilter配置

  1. import com.google.gson.Gson;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.apache.log4j.Logger;
  4. import com.entity.User;
  5. import javax.servlet.*;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. import java.io.IOException;
  10. /**
  11. * @about session过滤器,处理登录拦截
  12. */
  13. public class SessionFilter implements Filter {
  14. private static final Logger logger = LoggerFactory.getLogger(SessionFilter.class);
  15. /**
  16. * ajax请求标志
  17. */
  18. private static final String XMLHTTPREQUEST = "XMLHttpRequest";
  19. Gson gson = new Gson();
  20. FilterConfig config = null;
  21. //定义需要忽略的请求
  22. private String[] ignoreArr = null;
  23. @Override
  24. public void destroy() {
  25. this.config = null;
  26. }
  27. @Override
  28. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
  29. FilterChain filterChain) throws IOException, ServletException {
  30. HttpServletRequest request = (HttpServletRequest) servletRequest;
  31. HttpServletResponse response = (HttpServletResponse) servletResponse;
  32. boolean flag = isIgonre(request);
  33. //需要忽略的请求
  34. if (flag) {
  35. filterChain.doFilter(servletRequest, servletResponse);
  36. } else {
  37. //登录页面请求
  38. String loginUrl = config.getInitParameter("loginAction");
  39. //获取真实请求地址
  40. GamsConfig.getConfig().contextPath = request.getContextPath();
  41. //如果是登录页或者自定义过滤的请求,放行
  42. if (requestUri.endsWith(loginStr)) {
  43. filterChain.doFilter(servletRequest, servletResponse);
  44. return;
  45. }
  46. //从session里面获取用户信息
  47. User user = (User) request.getSession().getAttribute("user");
  48. // 请求是否是XMLHttpRequest类型
  49. String type = request.getHeader("X-Requested-With") == null ? "" : request.getHeader("X-Requested-With");
  50. if (user == null) {
  51. if (StringUtils.equals(XMLHTTPREQUEST, type)) {
  52. //告诉ajax这是重定向
  53. response.setHeader("REDIRECT", "REDIRECT");
  54. } else {
  55. response.setHeader("SESSIONSTATUS", "TIMEOUT");
  56. response.sendRedirect(loginUrl);
  57. }
  58. //重定向登录页面地址
  59. response.setHeader("CONTEXTPATH", loginUrl);
  60. response.setStatus(HttpServletResponse.SC_FORBIDDEN);
  61. } else {
  62. filterChain.doFilter(servletRequest, servletResponse);
  63. }
  64. }
  65. }
  66. /**
  67. * Tomcat启动时调用,进行初始化
  68. */
  69. @Override
  70. public void init(FilterConfig config) throws ServletException {
  71. ignoreArr = config.getInitParameter("ignorePattern").split(",");
  72. this.config = config;
  73. //initWebSocket();
  74. }
  75. /**
  76. * 忽略拦截的部分
  77. */
  78. public boolean isIgonre(HttpServletRequest request) {
  79. String path = request.getRequestURI().toLowerCase();
  80. for (String ignore : ignoreArr) {
  81. if (path.contains(ignore)) {
  82. return true;
  83. }
  84. }
  85. return false;
  86. }
  87. }

前端通用JS设置

项目基础js(项目自定的,我这个项目是common.js)中引入该方法:

  1. //解决Ajax请求时无法重定向的问题
  2. $.ajaxSetup( {
  3. //设置ajax请求结束后的执行动作
  4. complete : function(XMLHttpRequest, textStatus) {
  5. // 通过XMLHttpRequest取得响应头,REDIRECT
  6. var redirect = XMLHttpRequest.getResponseHeader("REDIRECT");
  7. //若HEADER中含有REDIRECT说明后端想重定向
  8. if (redirect === "REDIRECT") {
  9. var win = window;
  10. while (win !== win.top){
  11. win = win.top;
  12. }
  13. //将后端重定向的地址取出来,使用win.location.href去实现重定向的要求
  14. win.location.href= XMLHttpRequest.getResponseHeader("CONTEXTPATH");
  15. }
  16. }
  17. });

web.xml配置

  1. <!--session-->
  2. <filter>
  3. <filter-name>sessionFilter</filter-name>
  4. <filter-class>com.filter.SessionFilter</filter-class>
  5. <init-param>
  6. <param-name>loginAction</param-name>
  7. <param-value>/heioky/login.action</param-value>
  8. </init-param>
  9. </filter>
  10. <filter-mapping>
  11. <filter-name>sessionFilter</filter-name>
  12. <url-pattern>*.action</url-pattern>
  13. </filter-mapping>

END


搞定~