码迷,mamicode.com
首页 > 编程语言 > 详细

springmvc拦截器验证登录时间

时间:2016-11-19 02:07:08      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:共享   conf   www   mvc   判断   name   localhost   tin   row   

在前一篇【Filter实现用户名验证】的随笔里,记录了如何使用filter

这次增加了拦截器实现

①filter实现用户登陆时验证用户名是否为null

②interceptor实现用户登陆时时间判断,在时间段外不能进入系统

③在时间段外跳转到静态画面

难点在于怎么调到静态画面而不被filter给拦截住

最后用了下面的方法:

  1. <filter>
  2.   <filter-name>SecurityServlet</filter-name>
  3.   <filter-class>com.msm2.filter.MyFilter</filter-class>
  4.   
  5.   <init-param>
  6.    <param-name>ignores</param-name>
  7.    <param-value>/index</param-value>
  8.   
  9. </init-param>
  10.  </filter>
  11.  <filter-mapping>
  12.   <filter-name>SecurityServlet</filter-name>
  13.   <url-pattern>/*</url-pattern>
  14.  </filter-mapping>

 

  1. private String prefixIignores = null;
  2. public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
  3.    throws IOException, ServletException {
  4.         ......
  5.         if (canIgnore(request)) { 
  6.          arg2.doFilter(request, response); 
  7.             return; 
  8.         }
  9.   ......
  10. }

 

  1. @Override
  2.  public void init(FilterConfig arg0) throws ServletException {
  3.   encode = "utf-8";
  4.   
  5.   String cp = arg0.getServletContext().getContextPath();
  6.   String ignoresParam = arg0.getInitParameter("ignores");
  7.   prefixIignores = cp + ignoresParam;
  8.  }
  9.  
  10.  @Override 
  11.     public void destroy() { 
  12.         prefixIignores = null; 
  13.     }
  14.  
  15.  private boolean canIgnore(HttpServletRequest request) { 
  16.         String url = request.getRequestURI(); 
  17.             if (url.startsWith(prefixIignores)) { 
  18.                 return true; 
  19.             } 
  20.         return false; 
  21.     }

 

  1. <mvc:interceptors>
  2.      <!-- 多个拦截器,顺序执行 -->
  3.      <!-- 拦截器1 登陆拦截 -->
  4.      <mvc:interceptor>
  5.         <mvc:mapping path="/login" /><!-- 可以写多个,如果为/*,将拦截所有的Controller -->
  6.         <bean class="com.msm2.interceptor.LoginInterceptor">
  7.              <!--startTimeStr 属性指定允许登陆的开始时间--> 
  8.                 <property name="startTimeStr"> 
  9.                     <value>7:00:00</value> 
  10.                 </property> 
  11.                 <!--endTimeStr 属性指定允许登陆的结束时间--> 
  12.                 <property name="endTimeStr"> 
  13.                     <value>21:00:00</value> 
  14.                 </property> 
  15.                 <!--outTimePageUrl  属性指定不在时间范围内时,提示页面的URL--> 
  16.                 <property name="outTimePageUrl"> 
  17.                     <value>http://localhost:8080/msm2/index/index.jsp 
  18.                     </value> 
  19.                 </property> 
  20.         </bean>
  21.      </mvc:interceptor>
  22.  </mvc:interceptors>

终于实现了不拦截静态画面了

将工程源码给出,希望给在springmvc学习路上的朋友有所帮助。

貌似zip没法共享。

springmvc拦截器验证登录时间

标签:共享   conf   www   mvc   判断   name   localhost   tin   row   

原文地址:http://www.cnblogs.com/ysloong/p/6079161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!