title: Springboot with Spring OAuth2
body: <p>I'm implementing OAuth2 on my application using Spring Boot and Spring Security OAuth with JDBC client support.</p>
  
  <p>I can generate tokens, when I make a POST request to <a href="http://localhost:8080/oauth/token" rel="nofollow noreferrer">http://localhost:8080/oauth/token</a> I get a valid response like:</p>
  
  <pre><code>{
      "access_token": "359e93b2-555a-477b-9a65-e5062314fc23",
      "token_type": "bearer",
      "refresh_token": "6fd1ae31-8129-4729-a86b-e756c453a58a",
      "expires_in": 899,
      "scope": "read"
  }
  </code></pre>
  
  <p>Now the strange this is, I cannot find this token anywhere in the database. If I make another request to /oauth/token I get the same token but with a lower value for <code>expires_in</code> as expected. I get to the conclusion that this token information must be stored somewhere but I can not find it.</p>
  
  <pre><code>01:24:41    SELECT * FROM dummy.oauth_access_token LIMIT 0, 1000    0 row(s) returned   0.000 sec / 0.000 sec
  </code></pre>
  
  <p>All OAuth related tables are empty except <code>oauth_client_details</code> which has the client details to generate the token.</p>
  
  <p>Here's my code.</p>
  
  <p><strong>AuthServerOAuth2Config</strong> </p>
  
  <pre><code>@Configuration
  public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter {
  
      private final AuthenticationManager authenticationManager;
      private final AppConfig appConfig;
  
      @Autowired
      public AuthServerOAuth2Config(AuthenticationManager authenticationManager, AppConfig appConfig) {
          this.authenticationManager = authenticationManager;
          this.appConfig = appConfig;
      }
  
      @Override
      public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
          clients.jdbc(appConfig.dataSource());
      }
  
      @Override
      public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
          security.checkTokenAccess("permitAll()");
      }
  
      @Override
      public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
          endpoints.authenticationManager(authenticationManager);
      }
  }
  </code></pre>
  
  <p><strong>AppConfig Class</strong></p>
  
  <pre><code>@Configuration
  @PropertySource(value = "classpath:application.properties")
  public class AppConfig {
  
      @Value("${spring.datasource.url}")
      private String datasourceUrl;
  
      @Value("${spring.database.driverClassName}")
      private String dbDriverClassName;
  
      @Value("${spring.datasource.username}")
      private String dbUsername;
  
      @Value("${spring.datasource.password}")
      private String dbPassword;
  
      @Bean
      public DataSource dataSource() {
          final DriverManagerDataSource dataSource = new DriverManagerDataSource();
  
          dataSource.setDriverClassName(dbDriverClassName);
          dataSource.setUrl(datasourceUrl);
          dataSource.setUsername(dbUsername);
          dataSource.setPassword(dbPassword);
  
          return dataSource;
      }
  
      @Bean
      public TokenStore tokenStore() {
          return new JdbcTokenStore(dataSource());
      }
  }
  </code></pre>
  
  <p>And here's my pom.xml in case it is of any help to find why there are no tokens persisted in the database.</p>
  
  <pre><code>&lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;1.5.6.RELEASE&lt;/version&gt;
      &lt;relativePath/&gt;
      &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  &lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
      &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;!-- Defining which version of Spring Framework we are using --&gt;
      &lt;spring-cloud.version&gt;Dalston.SR1&lt;/spring-cloud.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-oauth2&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-aws-context&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
          &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;io.springfox&lt;/groupId&gt;
          &lt;artifactId&gt;springfox-swagger2&lt;/artifactId&gt;
          &lt;version&gt;2.6.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;io.springfox&lt;/groupId&gt;
          &lt;artifactId&gt;springfox-swagger-ui&lt;/artifactId&gt;
          &lt;version&gt;2.4.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.validation&lt;/groupId&gt;
          &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>My question is, why are the generated tokens not being persisted to the mysql database and how can I fix it?</p>