title: Spring security with multiple authentication providers - UsernameNotFoundException
body: <p>We have a spring security configuration for 2 authentication providers:  One for human users, second for other webapps (via REST). See my previous question: <a href="https://stackoverflow.com/questions/24288467/spring-security-for-both-web-services-and-users/">Spring security for both web services and users</a></p>
  
  <p>Now, the problem is, when a webapp sends username + pass, spring security first tries the user authentication provider, fails, and then tries the rest authentication provider.</p>
  
  <p>This causes a <code>org.springframework.security.core.userdetails.UsernameNotFoundException</code> in the log, even if the rest authentication is eventually successful. Is there a way to prevent this from happening?</p>
  
  <p>The configuration is:</p>
  
  <p>security.xml:</p>
  
  <pre><code>&lt;security:http use-expressions="true"&gt;
      &lt;security:intercept-url pattern="/user/login"
          access="permitAll" /&gt;
               ...
      &lt;security:intercept-url pattern="/**"
          access="isAuthenticated()" /&gt;
  
      &lt;security:form-login
          authentication-success-handler-ref="userAuthenticationSuccessHandler" /&gt;
  
      &lt;security:logout logout-url="/user/logout"
          logout-success-url="/demo/user/logoutSuccess" /&gt;
  &lt;/security:http&gt;
  
  &lt;bean id="bCryptPasswordEncoder"
      class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" /&gt;
  
  &lt;security:authentication-manager alias="authenticationManager"&gt;
      &lt;security:authentication-provider
          ref="authenticationProvider"&gt;
      &lt;/security:authentication-provider&gt;
      &lt;security:authentication-provider
          ref="restAuthenticationProvider"&gt;
      &lt;/security:authentication-provider&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p>rest-security.xml:</p>
  
  <pre><code>&lt;security:http create-session="stateless"
      entry-point-ref="digestEntryPoint" pattern="/provider/**"
      use-expressions="true"&gt;
      &lt;security:intercept-url pattern="/provider/**"
          access="isAuthenticated()" /&gt;
  
      &lt;security:http-basic /&gt;
      &lt;security:custom-filter ref="digestFilter"
          after="BASIC_AUTH_FILTER" /&gt;
  &lt;/security:http&gt;
  
  &lt;bean id="digestFilter"
      class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter"&gt;
      &lt;property name="userDetailsService" ref="webappDetailsServiceImpl" /&gt;
      &lt;property name="authenticationEntryPoint" ref="digestEntryPoint" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="digestEntryPoint"
      class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint"&gt;
      &lt;property name="realmName" value="Contacts Realm via Digest Authentication" /&gt;
      &lt;property name="key" value="acegi" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Log:</p>
  
  <pre><code>22 Jun 2014 10:43:46 ERROR LoggingAspect - Unhandled exception caught: ...UserDetailsServiceImpl    loadUserByUsername
  org.springframework.security.core.userdetails.UsernameNotFoundException: User with loginName: *** doesnt exist
  at ...UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:30)
  at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:102)
  at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
  at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
  at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
  at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:168)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
  at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
  at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
  at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
  at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
  at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
  at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)
  22 Jun 2014 10:43:47  INFO LoggingAspect - Entering: ...WebappDetailsServiceImpl    Method name: loadUserByUsername Method arguments : [1]
  </code></pre>