title: OAuth2 refresh_token logic implementation in spring-security-oauth2
body: <p>I have successfully implemented the request for new token in OAuth2 for this request:</p>
  
  <pre><code>    curl --request POST --url https://some-autentication-server.com/token --header 'content-type: content-type'
  </code></pre>
  
  <p>with the body provided as:</p>
  
  <pre><code>{
    "grant_type"="password",
    "username"="username",
    "password"="password"
    "client_id"="my-client-id"
  }
  </code></pre>
  
  <p>After the authentication the resource server curl can be accessed as:</p>
  
  <pre><code>curl -i -H "authorization: Bearer token-received-from-auth-server" \ 
  -H "accept: application/json" \
  -H "request-id: abcdef" \
  -H "consent-status: optedIn" \
  -X GET https://my-resource-server.com/path
  </code></pre>
  
  <p>The configuration I've used in Spring Boot is this:</p>
  
  <pre><code>@EnableOAuth2Client
  @Configuration
  public class OauthClientConfig {
  
      @Bean
      public CloseableHttpClient httpClient() throws Exception {
          CloseableHttpClient httpClient = null;
          try {
  
              httpClient = HttpClientBuilder.create()
                           .setProxy(new HttpHost("PROXY_HOST_NAME", 3000, "http"))
                           .build();
  
          } catch (Exception e) {
              throw e;
          }
          return httpClient;
      }
  
      @Bean
      public ClientHttpRequestFactory clientHttpRequestFactory(CloseableHttpClient httpClient) throws Exception {
  
          ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
          ((HttpComponentsClientHttpRequestFactory) clientHttpRequestFactory)
                  .setReadTimeout(10000);
          ((HttpComponentsClientHttpRequestFactory) clientHttpRequestFactory).setConnectTimeout(10000);
          return clientHttpRequestFactory;
      }
  
      @Bean
      @Qualifier("restTemplate")
      @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
      public OAuth2RestOperations restTemplate(OAuth2ProtectedResourceDetails oAuth2Resource,
              ClientHttpRequestFactory clientHttpRequestFactory, AccessTokenProvider accessTokenProvider)
              throws Exception {
  
          Map&lt;String, String[]&gt; map = new HashMap&lt;&gt;();
          AccessTokenRequest tokenRequest = new DefaultAccessTokenRequest(map);
          OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(oAuth2Resource,
                  new DefaultOAuth2ClientContext(tokenRequest));
          restTemplate.setRequestFactory(clientHttpRequestFactory);
          restTemplate.setAccessTokenProvider(accessTokenProvider);
          return restTemplate;
      }
  
      @Bean
      public AccessTokenProvider accessTokenProvider(ClientHttpRequestFactory clientHttpRequestFactory,
              OAuth2ProtectedResourceDetails oAuth2Resource) throws Exception {
          ResourceOwnerPasswordAccessTokenProvider accessTokenProvider = new ResourceOwnerPasswordAccessTokenProvider();
          accessTokenProvider.supportsRefresh(oAuth2Resource);
          accessTokenProvider.setRequestFactory(clientHttpRequestFactory);
          return new AccessTokenProviderChain(Arrays.&lt;AccessTokenProvider&gt;asList(accessTokenProvider));
      }
  
      @Bean
      @Qualifier("oAuth2Resource")
      public OAuth2ProtectedResourceDetails oAuth2Resource() {
          ResourceOwnerPasswordResourceDetails oAuth2Resource = new ResourceOwnerPasswordResourceDetails();
          oAuth2Resource.setId("MY_ID");
          oAuth2Resource.setAccessTokenUri("TOKEN_URL");
          oAuth2Resource.setClientId("TOKEN_CLIENTID");
          oAuth2Resource.setClientSecret("TOKEN_CLIENT_SECRET");
          oAuth2Resource.setScope(new ArrayList&lt;String&gt;(Arrays.asList(new String[]{"read"})));
          oAuth2Resource.setUsername("TOKEN_USERNAME");
          oAuth2Resource.setPassword("TOKEN_PAZZWORD");
          oAuth2Resource.setTokenName("access_token");
          oAuth2Resource.setGrantType("password");
          return oAuth2Resource;
      }   
  }
  </code></pre>
  
  <p>This works fine for the new token request, but now I want to be able to write a logic for implementing the <strong><em>refresh_token</em></strong>. Ideally, I want to store the tokens before it's expiry time and as soon as the token expiry reaches to about 90% of its expiry time, the refresh token logic would run hit the authentication server to refresh the token. The refresh token logic would run in the background all the time. My question is how to implement this logic using the spring-security-oauth2 library? Is this logic already implemented in the library or do I have to manually write the logic myself?</p>