zoukankan      html  css  js  c++  java
  • springmvc拦截器验证登录时间

    在前一篇【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没法共享。

  • 相关阅读:
    Query on The Trees(hdu 4010)
    背单词(bzoj 4567)
    P2819 图的m着色问题
    P1605 迷宫
    P1230 智力大冲浪
    P1082 同余方程
    P3372 【模板】线段树 1
    P2626 斐波那契数列(升级版)
    长生诀
    写给我第一个喜欢的男孩的歌
  • 原文地址:https://www.cnblogs.com/ysloong/p/6079161.html
Copyright © 2011-2022 走看看