title: Spring Boot security - multiple authentication providers
body: <p>My problem is that I would like to have two Authentication providers</p>
  
  <p><strong>BEFORE:</strong>
  I have my UserDetailServiceImpl and I validated the users against DB(not sure what provider is it) The user got role according to the data in DB</p>
  
  <p><strong>NOW:</strong>
  I have used ActiveDirectoryLdapAuthentication provider as follows</p>
  
  <pre><code>  @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          super.configure(auth);
          auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService);
      }
  
      @Bean
      public AuthenticationManager authenticationManager() {
          return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
      }
      @Bean
      public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
         MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN);
          provider.setConvertSubErrorCodesToExceptions(true);
          provider.setUseAuthenticationRequestCredentials(true);
  
          return provider;
      }
  </code></pre>
  
  <p>I made it work, so I'm able to authenticate.</p>
  
  <p><strong>THE PROBLEM:</strong></p>
  
  <ol>
  <li>I'm unable now to login with the database users anymore, only LDAP now.</li>
  <li>The UserDetailsService is not used, so which role the user has? </li>
  <li>Is there way how to add some default role to LDAP authenticated user?</li>
  </ol>
  
  <p><strong>So how to enable both providers?
  How to add roles to user, authenticated through LDAP auth.provider?</strong></p>
  
  <p>I would also really appreciate "big picture" description of what is going on here(under the hood). What is the role of each of the classes used here, it is really unclear how does it work(AuthenticationManager, AuthenticationManagerBuilder, AuthenticationProvider etc.) Maybe it is just hidden by autoconfiguration and so, but even looking to Spring Security directly does not make me any wiser.</p>
  
  <p>Thanks for any hints </p>
  
  <p>UPDATE
  This code seems to be working properly</p>
  
  <pre><code>    @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider()).userDetailsService(userDetailService);
      }
  
      public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
          MyActiveDirectoryLdapAuthenticationProvider provider = new MyActiveDirectoryLdapAuthenticationProvider(domain, url, rootDN);
          provider.setConvertSubErrorCodesToExceptions(true);
          provider.setUseAuthenticationRequestCredentials(true);
          provider.setAuthoritiesMapper(new SimpleAuthorityMapper());
          return provider;
      }
  </code></pre>