title: How to persist oauth access tokens in spring security JDBC?
body: <p>When the user login I generate a token so when the user want to access information of RESTapi he will not login again , the code works but I have a problem.</p>
  
  <p>The token that spring generate works but when I recompile the code I wrote, this token doesn't work anymore and I should request a new token to use the "bearer $Token " is this a normal behavior or I should fix something in the settings?</p>
  
  <p>Example : </p>
  
  <p><code>curl -u test: http://localhost:8080/oauth/token -d "grant_type=password&amp;username=id&amp;password=pass"</code></p>
  
  <p><code>{"access_token":"a46c3cf4-6777-4a61-9f71-268be047c383","token_type":"bearer","refresh_token":"8ef69c18-1a9e-47c0-ba80-b51a34144e9a","expires_in":603005,"scope":"read write trust"}</code></p>
  
  <p>When I recompile the code :</p>
  
  <p><code>curl -u test: http://localhost:8080/oauth/token -d "grant_type=password&amp;username=id&amp;password=pass"</code></p>
  
  <p><code>{"access_token":"1a69f140-47ac-4dde-9786-1d4f14f9a389","token_type":"bearer","refresh_token":"be99d434-54a0-4273-add8-cccad95caec3","expires_in":604799,"scope":"read write trust"}</code></p>
  
  <p>this is my code :</p>
  
  <pre><code>import com.lms.entities.UsersEntity;
  import com.lms.repositories.UserRepository;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
  import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  import org.springframework.web.bind.annotation.RestController;
  
  import java.util.concurrent.TimeUnit;
  
  @Configuration
  @ComponentScan
  @EnableAutoConfiguration
  @RestController
  class SpringBackendScoutApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(SpringBackendScoutApplication.class, args);
      }
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServer extends ResourceServerConfigurerAdapter {
  
          @Override
          public void configure(HttpSecurity http) throws Exception {
              // @formatter:off
              http
                      .requestMatchers()
                      .antMatchers(
                              Constants.USERS, Constants.COMMENTS, Constants.FRIENDS_REQUEST,
                              Constants.MUSICS, Constants.PHOTOS, Constants.POSTS, Constants.VIDEOS,
                              Constants.PROFILE_PHOTOS, Constants.FRIENDS, "/"
                      ).and()
                      .authorizeRequests()
                      .anyRequest().access("#oauth2.hasScope('read')")
                      .and().logout();
              // @formatter:on
          }
  
          @Override
          public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
              resources.resourceId("sparklr");
          }
  
      }
  
      @Configuration
      @EnableAuthorizationServer
      protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
  
          @Autowired
          private AuthenticationManager authenticationManager;
  
          @Override
          public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
              endpoints.authenticationManager(authenticationManager);
          }
  
          @Override
          public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
              clients.inMemory()
                      .withClient("test")
                      .authorizedGrantTypes("password", "authorization_code", "refresh_token", "implicit")
                      .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                      .scopes("read", "write", "trust")
                      .resourceIds("sparklr")
                      .accessTokenValiditySeconds((int) TimeUnit.DAYS.toSeconds(7))
  
              ;
          }
  
      }
  
      @Configuration
      @EnableWebSecurity
      protected static class SecurityConfig extends WebSecurityConfigurerAdapter {
          @Autowired
          UserRepository userRepository;
  
          @Override
          protected void configure(AuthenticationManagerBuilder auth) throws Exception {
              for (UsersEntity user : userRepository.findAll())
                  if (user.getUsername() != null &amp;&amp; user.getPassword() != null)
                      auth.inMemoryAuthentication().withUser(user.getUsername()).password(user.getPassword()).roles("USER");
          }
  
          @Bean
          @Override
          public AuthenticationManager authenticationManagerBean()
                  throws Exception {
              return super.authenticationManagerBean();
          }
  
      }
  
  }
  </code></pre>