title: Spring security: Cannot permit access to resource when adding custom filter
body: <p>I have following configuration for my spring security</p>
  
  <pre><code>http
      // if I gonna comment adding filter it's gonna work as expected
      .addFilterBefore(tokenAuthenticationFilter, BasicAuthenticationFilter.class)
              .authorizeRequests()
              .antMatchers("/rest/_health")
              .permitAll()
  
              .anyRequest()
              .authenticated()
  
              .and()
              .csrf()
              .disable();
  </code></pre>
  
  <p>So without custom filter everything works as expected - I have access to /rest/_health and access denied to everything else.</p>
  
  <p>But when I'm adding this filter - matchers don't work and filter works even for 'permitAll' resources.</p>
  
  <p>Code from my filter looks like this:</p>
  
  <pre><code>@Override
  public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
      HttpServletRequest httpRequest = (HttpServletRequest) request;
      HttpServletResponse httpResponse = (HttpServletResponse) response;
  
      try {
          String token = httpRequest.getHeader(HttpHeaders.AUTHORIZATION);
  
          Authentication authentication = authenticationManager.authenticate(
                  new TokenBasedAuthentication(token)
          );
  
          SecurityContextHolder.getContext().setAuthentication(authentication);
  
          chain.doFilter(request, response);
      } catch (AuthenticationException ex) {
          authenticationEntryPoint.commence(httpRequest, httpResponse, ex);
      }
  }
  </code></pre>
  
  <p>Any suggestions?</p>