title: Oauth2 Java Config JDBC
body: <p>I have a working example of oauth2 and rest with inmemory authentication and authorization for the client and owner of the resource. I am trying to convert that to the JDBC authentication and authorization sans much success. I keep getting Bad Credentials error. The Filters security goes through is quite confusing for me to debug:-)</p>
  
  <p>Inserted is my java code which for now uses an inmemory authentication user, but not for the client. The schema is the same as <a href="https://github.com/spring-projects/spring-security-oauth/blob/master/spring-security-oauth2/src/test/resources/schema.sql" rel="nofollow noreferrer">this</a>.</p>
  
  <p>Is there a complete working example of oauth2.0 using jdbc authentication and authorization with <b><i>Java Configuration</i></b> somewhere?</p>
  
  <p>-------------------Code below--     </p>
  
  <pre><code>public class WebSecurityConfig extends OAuth2ServerConfigurerAdapter {
      private final static Logger logger = LogFactory.getLogger(WebSecurityConfig.class);
  
  
  
      private JdbcClientDetailsService jdbcClientDetailsService;
      private JdbcTokenStore jdbcTokenStore;
      private JdbcUserDetailsManagerConfigurer jdbcUserDetailsManagerConfigurer;
      // @formatter:off
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          OAuth2ServerConfigurer oAuth2ServerConfigurer = new OAuth2ServerConfigurer( ).tokenStore(jdbcTokenStore);
          http
              .requestMatchers()
                  .and()
              .authorizeRequests()
                  .antMatchers("/").permitAll()
                  .anyRequest().authenticated()
                  .and()
              .apply(oAuth2ServerConfigurer);
          http.setSharedObject(ClientDetailsService.class, jdbcClientDetailsService);
  
      }
      // @formatter:on
  
  //  // @formatter:off
  //  @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  //        int validityInSec = Integer.parseInt(validtityInSeconds);
  
          auth
                  .userDetailsService(new InMemoryUserDetailsManager(getUserDetails()));
  
      }
      // @formatter:on
  //
      private  final Collection&lt;UserDetails&gt; getUserDetails() {
          List&lt;UserDetails&gt; userDetails = new ArrayList&lt;UserDetails&gt;();
          userDetails.add(new User("auction", "password", AuthorityUtils.createAuthorityList(
                  "USER", "read", "write")));
          return userDetails;
      }
  
  
      @Autowired
      @Qualifier("oauth_details_ds")
      public void setDataSource(DataSource dataSource){
          jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
          jdbcTokenStore =  new JdbcTokenStore(dataSource);
  //        try {
  //            jdbcUserDetailsManagerConfigurer = new JdbcUserDetailsManagerConfigurer().dataSource(dataSource);
  //        } catch (Exception e) {
  //            logger.error("Bad code design: ",e);
  //            e.printStackTrace();
  //        }
      }
  
  
  
  }
  </code></pre>