session失效,登录页面在子界面跳转

方法一:

登录页面嵌入这一段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>loginfilterfilter-name>
    <filter-class>
        com.lty.ebus.custom.filters.CheckLoginFilterfilter-class>
    <init-param>
        <param-name>rootPathparam-name>
        <param-value>/login.jspparam-value>
    init-param>
filter>

<filter-mapping>
    <filter-name>loginfilterfilter-name>
    <url-pattern>/webviews/*url-pattern> 
filter-mapping>

<filter-mapping>
    <filter-name>loginfilterfilter-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("");
        } 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判断解析数据判断,弹窗跳转

session失效,登录页面在子界面跳转_第1张图片

你可能感兴趣的:(session失效,登录页面在子界面跳转)