title: Spring Security - multiple authentication-providers
body: <p>My web app has multiple authentication managers (one for API one for WEB access). The api should have a basic auth service only - configured via the spring security markup as seen below:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:security="http://www.springframework.org/schema/security"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt;
  
      &lt;security:authentication-manager alias="apiAuthenticationManager"&gt;
          &lt;security:authentication-provider ref="apiAuthenticationProvider" /&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;security:authentication-provider &gt;
          &lt;security:user-service&gt;
              &lt;security:user name="apiadmin" password="password" authorities="ROLE_API_ADMIN" /&gt;
              &lt;security:user name="apiuser" password="otherpassword" authorities="ROLE_API_USER" /&gt;
          &lt;/security:user-service&gt;
      &lt;/security:authentication-provider&gt;
  ...
  </code></pre>
  
  <p>i can not inline the authentication-provider since i want it to be overrideable by child-bean configs.</p>
  
  <p>my problem is that i can not define an alias/id on the security:authentication-provider element to reference it in the authentication-manager. Is there an easy workaround for this?</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>i finally figured out how to do it using the namespace-way without diving into plain bean config :)</p>
  
  <pre><code>&lt;security:user-service id="apiUserDetailsService"&gt; 
      &lt;security:user name="apiadmin" password="password" authorities="ROLE_API_ADMIN" /&gt;
      &lt;security:user name="apiuser" password="otherpassword" authorities="ROLE_API_USER" /&gt;
      &lt;/security:user-service&gt;
  
  &lt;security:authentication-manager alias="apiAuthenticationManager"&gt;
      &lt;security:authentication-provider user-service-ref="apiUserDetailsService"/&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>