java中session过期后跳转登录页面。

后台拦截器:

/**
 * 拦截器
 */
public class SystemInterceptor extends HandlerInterceptorAdapter{
    
    private static Logger log = LogManager.getLogger();
    
    @Override  
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception{
        
        String uri = request.getRequestURI();
        log.debug("Pre-handle url="+uri);
        
        if (uri.startsWith(request.getContextPath()+"/login/tologin") ||
            uri.startsWith(request.getContextPath()+"/login/loginPage") ||
            uri.startsWith(request.getContextPath()+"/login/verifyCodeImg")) {
            
            log.debug(uri + " 不进行拦截");
            
        }else{
            
            //session失效,到登录页面
            HttpSession session = request.getSession();
            String userId = (String) session.getAttribute(Constant.SESSION_USER_NAME_STRING);
            
            if(userId == null || userId.equals("")){
                
                //AJAX方法访问
                if (handler instanceof HandlerMethod) {  
                    if (((HandlerMethod) handler).getMethod().isAnnotationPresent(ResponseBody.class)) {// 有权限控制的就要检查
                        
                        //response.sendRedirect(request.getContextPath()+"/ajaxSessionOut.txt");
                        //response.sendRedirect(request.getContextPath());
                        response.getWriter().print("SESSION_OUT");
                    }else{
                        //非ajax请求,页面跳转请求
                        response.sendRedirect(request.getContextPath());
                    }
                }else{
                    //非ajax请求,页面跳转请求
                    response.sendRedirect(request.getContextPath());
                }
                
                log.debug("SESSION 失效 返回登陆页面");
                
                return false;
            }
        }
        
        return true;
    }  
      
    private HandlerMethod HandlerMethod(Object handler) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override  
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)throws Exception{  
        //logger.warn("Post-handle");
        //System.out.println("Post-handle");  
    }  
      
    @Override  
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex){  
        //logger.warn("After completion handle");
        //System.out.println("After completion handle");  
    }
}

前台ajax每次请求都会执行的一段js

$(document).ajaxComplete(function(event, xhr, settings){
    if(xhr.responseText && xhr.responseText == "SESSION_OUT"){
        //alert("SESSION_OUT");
        //ajax 访问 session 失效
        window.location.href = "**********";//这边写跳转登录页面网址
    }
});

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值