title: Multiple Spring Security configurations for REST via BasicAuth vs. Session with CSRF
body: <p>In am working on a <strong>Spring Boot (1.3.2)</strong> application which just serves Spring MVC REST controllers that are consumed by a <strong>JavaScript single page app</strong> (deployed standalone, not inside the boot jar). The setup uses <strong>Spring Security</strong>, <strong>Spring Session</strong> and has <strong>CSRF protection</strong> enabled.</p>
  
  <p>This works as expected for the JavaScript client. A login is done by a GET call with Basic Auth provided, the backend creates a session, returns a cookie and also provides the CSRF token in the response according to this blog post at <a href="https://spring.io/guides/tutorials/spring-security-and-angular-js/" rel="nofollow noreferrer">https://spring.io/guides/tutorials/spring-security-and-angular-js/</a> (except that the custom filter is way more behind in the chain to obtain the correct token after the <code>SessionManagementFilter</code> - which is not that relevant for this question I think).</p>
  
  <p>Until here, everything works as intended pretty elegantly without any workarounds.</p>
  
  <p>Now we also want to <strong>expose some of the interfaces for mobile/cli clients</strong>. These clients should be able to access a subset of the interfaces via Basic Auth. So the goal is to allow non-readonly calls (POST, PUT, DELETE) for these clients by just providing the credentials via Basic Auth. As they provide the credentials in their request, it is not neccessary for them to provide a CSRF token.</p>
  
  <p>So my first idea was to look for some way to conditionally skip some filters (the csrf) if there was a successful authentication by the provided Basic Auth. It seems that the existing filters are not meant to be used in that way and this would also somehow violate the concept of Spring Security.</p>
  
  <p>I then thought to have found a solution by using the concept explained in the Spring Security docs (<a href="http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity" rel="nofollow noreferrer">http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#multiple-httpsecurity</a>) that uses separate <code>WebSecurityConfigurerAdapter</code>s as also described in <a href="https://stackoverflow.com/questions/31145518/spring-boot-web-app-w-both-session-csrf-stateless-basic-auth-w-o-csrf">Spring Boot web app w/ both session + CSRF &amp; stateless Basic Auth w/o CSRF</a> or 
  <a href="https://stackoverflow.com/questions/27571178/how-can-i-configure-spring-security-for-multiple-servlets">How can I configure spring security for multiple servlets?</a> </p>
  
  <p>So currently I have two <code>WebSecurityConfigurerAdapter</code>s with different Orders and disjunct request matchers, both overriding <code>configure(AuthenticationManagerBuilder auth)</code> and <code>configure(HttpSecurity http)</code>. I therefore aliased the relevant REST controllers by using <code>@RequestMapping({"/things", "/aliased/things"})</code> to be able to get separate security filter chains.</p>
  
  <p>This works in a way that depending on the Path ("things" or "aliased/things") the according filter chain is used. But somehow this setup is still corrupt as I now can do a "login" on the regular path, which creates a session and with this session, I now can also call one of the second filter chain's path which now results in a correct authentication (probably due to the <code>SecurityContextPersistenceFilter</code>) which authenticates me and allows non CSRF-protected calls to the interfaces on the chain which should be used by the mobile clients only.</p>
  
  <p>This seems not to be the way to handle such a case. I am interested in finding hints how to handle that requirement - which in my opinion should be some common use case. Probably I was running way to far in the wrong direction with my experiments.</p>