• jQueryrocket


    方法一:

    登录页面嵌入这一段js

    if (top != window){
    top.location.href = window.location.href; 
    // window.parent.location.href="${pageContext.request.contextPath}/login.jsp";
    // window.parent.location.href="${pageContext.request.contextPath}/login.jsp";
    }

    方法二:

    使用Filter,同时过滤对静态页面和controller的访问(非ajax)

    web.xml配置

    <filter>
        <filter-name>loginfilter</filter-name>
        <filter-class>
            com.lty.ebus.custom.filters.CheckLoginFilter</filter-class>
        <init-param>
            <param-name>rootPath</param-name>
            <param-value>/login.jsp</param-value>
        </init-param>
    </filter>
    <!-- 所有需要session才能访问的JSP或HTML页面均放在webviews下-->
    <filter-mapping>
        <filter-name>loginfilter</filter-name>
        <url-pattern>/webviews/*</url-pattern> 
    </filter-mapping>
    <!-- 过滤controller -->
    <filter-mapping>
        <filter-name>loginfilter</filter-name>
        <url-pattern>/webapp/*</url-pattern>
    </filter-mapping>

     过滤器

    public class CheckLoginFilter implements Filter {
    
        private String rootPath;
    
        public void destroy() {
            if (!StringUtils.isEmpty(rootPath)) {
                this.rootPath = null;
            }
        }
    
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            HttpServletRequest request = (HttpServletRequest) req;
            Object session = RedisHelper.get(SysGlobalConstants.SESSIONID.concat(request.getSession().getId()));
            String uri = request.getRequestURI().toLowerCase();
            if (StringUtils.isEmpty(session) && uri.indexOf("login") < 0 && uri.indexOf("logout") < 0) {
                // 设置header,便于ajax请求做处理
                response.setHeader("sessionstatus", "timeout");
                response.setContentType("text/html;charset=UTF-8");
                response.getWriter()
                        .println("<script language='javascript'>if(window.opener==null){window.top.location.href='"
                                + rootPath + "';}else{window.opener.top.location.href='" + rootPath
                                + "';window.close();}</script>");
            } else {
                chain.doFilter(req, res);
            }
        }
    
        public void init(FilterConfig con) throws ServletException {
            this.rootPath = con.getServletContext().getContextPath().concat(con.getInitParameter("rootPath"));
        }
    
    }

    方法三:

    如果是ajax请求 那种,除了上面的还要往后看。该js文件需要被引入到有ajax请求(对session有要求)的页面中(其实思路上和第一种是差不多的)。

    JS

    /** 
     * 设置未来(全局)的AJAX请求默认选项 
     * 主要设置了AJAX请求遇到Session过期的情况 
     */  
    var appName = $("#appName").val();
    
    $.ajaxSetup({  
        complete: function(xhr,status) { 
            var sessionStatus = xhr.getResponseHeader('sessionstatus');  
            if(sessionStatus == 'timeout') {  
                var top = getTopWinow();  
                top.location.href = appName + '/login.jsp';              
            }  
        }  
    });  
      
    /** 
     * 在页面中任何嵌套层次的窗口中获取顶层窗口 
     * @return 当前页面的顶层窗口对象 
     */  
    function getTopWinow(){  
        var p = window;  
        while(p != p.parent){  
            p = p.parent;  
        }  
        return p;  
    }

      方法四:

    在ajax判断解析数据判断,弹窗跳转

  • 相关阅读:
    linux 文件搜索
    解决android 无法打开 DDMS 中的data目录
    JAVA 截图+tess4j识别
    JAVA 获取网页源代码保存到本地文件
    java连接sqlserver数据简单操作
    SQL server 2008 安装提示:属性不匹配
    SQLServer 安装提示需要重启计算机的解决方案
    Android蓝牙----打开,关闭操作
    JAVA中String类的比较
    Android中的AlertDialog和ProgressDialog用法
  • 原文地址:https://www.cnblogs.com/kaspar/p/12557355.html
Copyright © 2020-2023  润新知