title: How to intercept resources using spring interceptors or filter
body: <p>My config in applicationContext.xml is as follows:</p>
  
  <pre><code> &lt;mvc:annotation-driven/&gt;
      &lt;context:component-scan base-package="com.lixindi.gradproject"/&gt;
      &lt;mvc:resources mapping="login/**" location="login/"/&gt;
      &lt;mvc:resources mapping="admin/**" location="admin/"/&gt;
      &lt;mvc:resources mapping="vote/**" location="vote/"/&gt;
  
      &lt;mvc:interceptors&gt;
          &lt;mvc:interceptor&gt;
              &lt;mvc:mapping path="/admin/**"/&gt;
              &lt;bean class="com.lixindi.gradproject.interceptor.LoginInterceptor"/&gt;
          &lt;/mvc:interceptor&gt;
      &lt;/mvc:interceptors&gt;
  </code></pre>
  
  <p>LoginInterceptor:</p>
  
  <pre><code>public class LoginInterceptor extends HandlerInterceptorAdapter {
      public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
          if (httpServletRequest.getSession().getAttribute("user") == null) {
              httpServletResponse.sendRedirect("/login/login.html");
              System.out.println("invoked");
              return false;
          } else {
              return true;
          }
      }
  }
  </code></pre>
  
  <p>There are several html files under webapp/admin which I do not want to go through DispatcherServlet. And the interceptor does not intercept requests, for example, <a href="http://localhost:8080/admin/admin.html" rel="nofollow noreferrer">http://localhost:8080/admin/admin.html</a>. But I want any url including resources to be intercepted to make sure users are logged in. How could I make this work?</p>
  
  <p>Thanks @Serge Ballesta.</p>
  
  <p>However, it still cannot intercept resources, for example, <a href="http://localhost:8080/admin/admin.html" rel="nofollow noreferrer">http://localhost:8080/admin/admin.html</a>.</p>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;filter&gt;
          &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;com.lixindi.gradproject.filter.LoginFilter&lt;/filter-class&gt;
  
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;CharacterEncodingFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>LoginFilter:</p>
  
  <pre><code>public class LoginFilter implements Filter {
  public void init(FilterConfig filterConfig) throws ServletException {
  
  }
  
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      HttpServletRequest request = (HttpServletRequest) servletRequest;
      HttpServletResponse response = (HttpServletResponse) servletResponse;
      System.out.println("filter");
      if (request.getSession().getAttribute("user") == null) {
  
          response.sendRedirect("/login/login.html");
      } else {
          filterChain.doFilter(servletRequest, servletResponse);
      }
  }
  
  public void destroy() {
  
  }
  </code></pre>
  
  <p>}</p>