过滤器(当前登录人的信息(session过期时)进行重新登录)

阅读更多
第一步:书写过滤器类,过滤器必须继承org.springframework.web.servlet.handler.HandlerInterceptorAdapter并重写preHandle方法。

/**
* 检测是否已经登录
*

Title:SecurityInterceptor


*

Description:


* @author zhanghaodong
* @date 2015年11月27日 上午11:18:56
*/
public class SecurityInterceptor extends HandlerInterceptorAdapter {

        @Override 
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { 
            if((request.getContextPath().replace("/", "")).equals((request.getRequestURI().replace("/", "")))){
                return true;
            }
            HttpSession session = request.getSession(true); 
            // 从session 里面获取用户名的信息 
            Object obj =session.getAttribute(WebCnts.SESSION_KEY_LOGINUSER);
            // 判断如果没有取到用户信息,就跳转到登陆页面,提示用户进行登陆 
            if (obj == null || "".equals(obj.toString())) { 
                  response.setContentType("text/html"); 
                    response.setCharacterEncoding("utf-8"); 
                    PrintWriter out = response.getWriter();   
                    StringBuilder builder = new StringBuilder();   
                    builder.append("");   
                    out.print(builder.toString());   
                    out.close();   
                    return false; 
            } 
            return true; 
        } 

        @Override 
        public void postHandle(HttpServletRequest req, HttpServletResponse res, Object arg2, ModelAndView arg3) throws Exception { 
        } 

        @Override 
        public void afterCompletion(HttpServletRequest req, HttpServletResponse res, Object arg2, Exception arg3) throws Exception { 
        } 

}



第二步:在spring-mvc.xml文件中添加下面代码(过滤的路径)

   
       
          
           
    
             
             
             
             
             
             
           
       

   
   

你可能感兴趣的:(过滤器,session过期)