title: Java Spring Security config - multiple authentication providers
body: <p>There are several references of multiple authentication providers in spring security, but no example in Java config could be located.</p>
  
  <p>The following link gives the XML notation:
  <a href="https://stackoverflow.com/questions/9303502/multiple-authentication-providers-in-spring-security">Multiple Authentication Providers in Spring Security</a></p>
  
  <p>We need to authenticate using LDAP or DB </p>
  
  <p>Below is our sample code:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  @EnableWebSecurity
  public class XSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private AuthenticationProvider authenticationProvider;
  
      @Autowired
      private AuthenticationProvider authenticationProviderDB;
  
  
      @Override
      @Order(1)
  
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(authenticationProvider);
      }
  
  
      @Order(2)
      protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(authenticationProviderDB);
      }
  
      @Override
        public void configure(WebSecurity web) throws Exception {
          web
            .ignoring()
               .antMatchers("/scripts/**","/styles/**","/images/**","/error/**");
        }
      ______
  
      @Override
      @Order(1)
      protected void configure(HttpSecurity http) throws Exception {
          http.csrf().disable()
          .authorizeRequests()
              .antMatchers("/","/logout","/time").permitAll()
                      .antMatchers("/admin").hasRole("ADMIN")         
                          .anyRequest().authenticated()
              .and()
          .formLogin()
              .loginPage("/index")
              .loginProcessingUrl("/perform_login")
              .usernameParameter("email")
              .passwordParameter("password")
              .failureUrl("/index?failed=true")
              .defaultSuccessUrl("/summary",true)
              .permitAll()
              .and()
           .logout().logoutUrl("/logout")
                    .logoutSuccessUrl("/index?logout=true").permitAll()
              .and()
              .exceptionHandling().accessDeniedPage("/error403")
          .and().authenticationProvider(authenticationProvider);
  
      }
  
      @Order(1)
      protected void configureDB(HttpSecurity http) throws Exception {
          http.csrf().disable()
          .authorizeRequests()
              .antMatchers("/","/logout").permitAll()
              .anyRequest().authenticated()
              .and()
          .formLogin()
              .loginPage("/index")
              .loginProcessingUrl("/perform_login")
              .usernameParameter("email")
              .passwordParameter("password")
              .failureUrl("/index?failed=true")
              .defaultSuccessUrl("/summary",true)
              .permitAll()
              .authenticationProvider(authenticationProviderDB)
      //This line giving compilation error stating authenticationProvider is not available in formloginconfigurer
  
           .and()
           .logout().logoutUrl("/logout")
                    .logoutSuccessUrl("/index?logout=true").permitAll()
              .and()
              .exceptionHandling().accessDeniedPage("/error403");
      }
  
  }
  </code></pre>