title: Configure JDBC Authentication in Spring Boot
body: <p>Goal:  Add jdbc authentication to spring boot with default security configurations.</p>
  
  <p>Source can be found <a href="https://github.com/mosgjig/user" rel="nofollow">here</a></p>
  
  <p>Per <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-security.html" rel="nofollow">Spring Boot Docs</a></p>
  
  <blockquote>
    <p>configure the global AuthenticationManager by autowiring an AuthenticationManagerBuilder into a method in one of your @Configuration classes</p>
  </blockquote>
  
  <p>and <a href="http://docs.spring.io/spring-security/site/docs/4.0.3.RELEASE/reference/htmlsingle/#jc-authentication-jdbc" rel="nofollow">Spring Security Docs</a> with example:</p>
  
  <pre><code>@Autowired
  private DataSource dataSource;
  
  @Autowired
  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth
          .jdbcAuthentication()
              .dataSource(dataSource)
              .withDefaultSchema()
              .withUser("user").password("password").roles("USER").and()
              .withUser("admin").password("password").roles("USER", "ADMIN");
  }
  </code></pre>
  
  <p>Given the above, added the following (<a href="https://github.com/mosgjig/user/blob/master/src/main/java/com/msyla/usergreeter/user/config/AuthenticationManagerConfig.java" rel="nofollow">found here</a>):</p>
  
  <pre><code>@Configuration
  @EnableAuthorizationServer
  @EnableResourceServer
  public class AuthenticationManagerConfig {
  
      @Autowired
      private DataSource dataSource;
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
          auth.jdbcAuthentication()
                  .dataSource(dataSource)
                  .withDefaultSchema()
                  .withUser("user").password("password").roles("ROLE_USER").and()
                  .withUser("admin").password("password").roles("ROLE_USER", "ROLE_ADMIN");
      }
  }
  </code></pre>
  
  <p>with resulting error:</p>
  
  <pre><code>java.lang.IllegalStateException: Cannot apply org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer@13404f75 to already built object
  </code></pre>
  
  <p>and further up the logs, we can see the auto config doing it's thing when it shouldn't be:</p>
  
  <pre><code>2016-02-02 22:52:48.047 DEBUG 30487 --- [ost-startStop-1] eGlobalAuthenticationAutowiredConfigurer : Eagerly initializing {org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration=org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration$$EnhancerBySpringCGLIB$$627430a8@78acaa86}
  2016-02-02 22:52:48.074 DEBUG 30487 --- [ost-startStop-1] .s.BootGlobalAuthenticationConfiguration : Eagerly initializing {user=com.msyla.usergreeter.user.User$$EnhancerBySpringCGLIB$$3cd414fd@73128671, coreConfig=com.msyla.usergreeter.user.core.config.CoreConfig$$EnhancerBySpringCGLIB$$30c07250@6ed3c66b}
  2016-02-02 22:52:48.095  INFO 30487 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration : 
  
  Using default security password: 5b33158f-156d-43f4-892f-6c452f15e1cc
  </code></pre>
  
  <p>Question:  All that is being asked here is to have default boot configurations with the exception of where the user is being stored, jdbc instead of in memory. Am I doing it incorrectly?  Are the docs wrong?  I have tried several other routes to no avail.  </p>
  
  <p>Thanks in advance!</p>