title: Spring 4 - Add a custom filter before BasicAuthenticationFilter, but only for certain URL patterns
body: <p>I'm using Spring/Security 4.x with Java configuration.
  I have several URLs. All of them are authenticated using BASIC authentication. Assume URL patterns are,</p>
  
  <ul>
  <li>/urlPattern1/** </li>
  <li>/urlPattern2/** </li>
  <li>/urlPattern3/**</li>
  </ul>
  
  <p>Everything works fine so far. </p>
  
  <p>Now I want to add a CORS filter only for the pattern /urlPattern3/**. If I add the custom CORS filter (I tried with few ways), I end up with <code>AccessDeniedException</code>. Ajax <code>pre-flight</code> request fails at the <code>BasicAuthenticationFilter</code> before it reaches to custom CORS filter. </p>
  
  <p>I found that the custom filter which I added is at the bottom of the filter chain. Therefore I added it before <code>BasicAuthenticationFilter</code>.</p>
  
  <p>If I add the CORS filter using <code>http.addFilterBefore(new CORSFilter(), BasicAuthnticationFilter.class)</code>, incoming requests which match to every other URL patterns also go through this custom CORSFilter, which is not what I want. (But it works)</p>
  
  <p>What I want to do <em>now</em> is,</p>
  
  <ol>
  <li>Add a custom filter before any known filter (i.e. <code>BasicAuthenticationFilter.class</code>)</li>
  <li>Specify URL patterns for that custom filter </li>
  </ol>