SpringMVC中Session超时拦截器配置

来源:互联网 发布:数据库防护 编辑:程序博客网 时间:2024/06/05 20:05

1、在spring配置文件中配置对应的拦截器,下面是我的拦截器配置

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 拦截器配置 -->  
  2.     <mvc:interceptors>  
  3.         <!-- session超时 -->  
  4.         <mvc:interceptor>  
  5.             <mvc:mapping path="/*/*" />  
  6.             <bean class="tm.change.www.interceptor.SessionTimeoutInterceptor">  
  7.                 <property name="allowUrls">  
  8.                     <list>  
  9.                         <!-- 如果请求中包含以下路径,则不进行拦截 -->  
  10.                         <value>/login</value>  
  11.                         <value>/js</value>  
  12.                         <value>/css</value>  
  13.                         <value>/image</value>  
  14.                         <value>/images</value>  
  15.                     </list>  
  16.                 </property>  
  17.             </bean>  
  18.         </mvc:interceptor>  
  19.     </mvc:interceptors>  
2、写出对应的sessionTimeoutInterceptor类
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /**  
  2.  * session拦截器  
  3.  *   
  4.  * @author tianyong  
  5.  *   
  6.  */  
  7. public class SessionTimeoutInterceptor implements HandlerInterceptor {  
  8.   
  9.     public String[] allowUrls;// 还没发现可以直接配置不拦截的资源,所以在代码里面来排除  
  10.   
  11.     public void setAllowUrls(String[] allowUrls) {  
  12.         this.allowUrls = allowUrls;  
  13.     }  
  14.   
  15.     @Override  
  16.     public void afterCompletion(HttpServletRequest arg0,  
  17.             HttpServletResponse arg1, Object arg2, Exception arg3)  
  18.             throws Exception {  
  19.         // TODO Auto-generated method stub  
  20.   
  21.     }  
  22.   
  23.     @Override  
  24.     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,  
  25.             Object arg2, ModelAndView arg3) throws Exception {  
  26.         // TODO Auto-generated method stub  
  27.   
  28.     }  
  29.   
  30.     @Override  
  31.     public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object arg2) throws Exception {  
  32.         String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");  
  33. //      System.out.println(requestUrl);  
  34.         if (null != allowUrls && allowUrls.length >= 1)  
  35.             for (String url : allowUrls) {  
  36.                 if (requestUrl.contains(url)) {  
  37.                     return true;  
  38.                 }  
  39.             }  
  40.             User user = (User) request.getSession().getAttribute("user");  
  41.         if (user != null) {  
  42.             return true; // 返回true,则这个方面调用后会接着调用postHandle(), afterCompletion()  
  43.         } else {  
  44.             // 未登录 跳转到登录页面  
  45.             throw new SessionTimeoutException();// 返回到配置文件中定义的路径  
  46.         }  
  47.     }  
  48.   
  49. }  
3、因为未登录的时候我们抛出了一个错误,我们可以在配置文件中拦截这个错误,已完成session销毁跳转
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 自定义异常处理,SimpleMappingExceptionResolver这个类可以是个空类,但是要写,方便在java代码里面使用 -->  
  2.     <bean id="exceptionResolver"  
  3.         class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
  4.         <property name="exceptionMappings">  
  5.             <props>  
  6.                 <prop key="tm.change.www.interceptor.SessionTimeoutException">redirect:../index.jsp</prop>  
  7.             </props>  
  8.         </property>  
  9.     </bean>  

而SessionTimeoutException只有一个类的壳子就行,继承exception,主要作用是出错之后跳转到index页面

4、此方法在iframe中登录页面将会在子窗口中打开,这是一个问题,还没有解决

5、另一种方法如下

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /**  
  2.  * spring拦截器  
  3.  * @author tianyong  
  4.  *  
  5.  */  
  6. public class SecurityInterceptor implements HandlerInterceptor{  
  7.   
  8.     public String[] allowUrls;// 还没发现可以直接配置不拦截的资源,所以在代码里面来排除  
  9.   
  10.     public void setAllowUrls(String[] allowUrls) {  
  11.         this.allowUrls = allowUrls;  
  12.     }  
  13.   
  14.     @Override  
  15.     public void afterCompletion(HttpServletRequest arg0,  
  16.             HttpServletResponse arg1, Object arg2, Exception arg3)  
  17.             throws Exception {  
  18.         // TODO Auto-generated method stub  
  19.   
  20.     }  
  21.   
  22.     @Override  
  23.     public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,  
  24.             Object arg2, ModelAndView arg3) throws Exception {  
  25.         // TODO Auto-generated method stub  
  26.   
  27.     }  
  28.   
  29.     @Override  
  30.     public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object arg2) throws Exception {  
  31.         String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");  
  32.         response.setContentType("text/html; charset=utf-8");  
  33.         HttpSession session = request.getSession(true);  
  34. //      System.out.println(requestUrl);  
  35.         if (null != allowUrls && allowUrls.length >= 1)  
  36.             for (String url : allowUrls) {  
  37.                 if (requestUrl.contains(url)) {  
  38.                     return true;  
  39.                 }  
  40.             }  
  41.         Object obj = session.getAttribute(SystemConstants.SEESION_IUSER);  
  42.         if (obj == null || "".equals(obj.toString())) {  
  43. //          throw new SessionTimeoutException();// 返回到配置文件中定义的路径  
  44. //          response.sendRedirect(SystemConstants.LOGIN_URL);  
  45.             PrintWriter out = response.getWriter();    
  46.             StringBuilder builder = new StringBuilder();    
  47.             builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");    
  48.             builder.append("alert(\"页面过期,请重新登录\");");    
  49.             builder.append("window.top.location.href=\""+SystemConstants.LOGIN_URL+"\"");    
  50.             builder.append("</script>");    
  51.             out.print(builder.toString());    
  52.             out.close();    
  53.               
  54.         }   
  55.         return true; // 返回true,则这个方面调用后会接着调用postHandle(), afterCompletion()  
  56.     }  
以上解决了iframe中登录页面出现位置错误问题,当session超时时候用js强制跳转到登录页面中


转自:http://blog.csdn.net/top_y/article/details/50817399

0 0