title: Will OAuth2RestTemplate refresh itself when token expires in the middle of multiple requests in 1 thread?
body: <p>so I will be still using the deprecated OAuth2RestTemplate for this 1 feature (in some time we will move to WebClient but as of now we need do use OAuth2RestTemplate).</p>
  <p>The use case is that we do requests on behalf of the client, they send request to our API with their credentials for some other stuff and we use their keys for external API request as well.</p>
  <p>So this external API gives back access_tokens and everything is working well using it like in the code. The expires_in is set for a month for the token so it never really expires in the development cycle.</p>
  <p>Will this current code work when the access_token expires while doing multiple requests?</p>
  <ol>
  <li>Init resttemplate</li>
  <li>Request 1 (access token is valid, not expired) - works</li>
  <li>Now second request in the for loop, now the token actually expired</li>
  <li>What will happen now? Will it automatically go and obtain new one or this has be be configured someway?</li>
  </ol>
  <pre><code>ResourceOwnerPasswordResourceDetails details = new ResourceOwnerPasswordResourceDetails();
                  details.setAccessTokenUri(accessTokenUri);
                  details.setScope(Collections.singletonList(SCOPE));
                  details.setGrantType(PASSWORD_GRANT);
                  details.setUsername(clientKey);
                  details.setPassword(password);
                  details.setClientAuthenticationScheme(AuthenticationScheme.header);
                  details.setAuthenticationScheme(AuthenticationScheme.header);
  
                  OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(details, new DefaultOAuth2ClientContext()); 
  </code></pre>
  <p>In the AccessTokenProviderChain.java file I found following code. Will it get run if that happens?</p>
  <pre><code>if (resource.isClientOnly() || (auth != null &amp;&amp; auth.isAuthenticated())) {
              existingToken = request.getExistingToken();
              if (existingToken == null &amp;&amp; clientTokenServices != null) {
                  existingToken = clientTokenServices.getAccessToken(resource, auth);
              }
  
              if (existingToken != null) {
                  if (existingToken.isExpired()) {
                      if (clientTokenServices != null) {
                          clientTokenServices.removeAccessToken(resource, auth);
                      }
                      OAuth2RefreshToken refreshToken = existingToken.getRefreshToken();
                      if (refreshToken != null &amp;&amp; !resource.isClientOnly()) {
                          accessToken = refreshAccessToken(resource, refreshToken, request);
                      }
                  }
                  else {
                      accessToken = existingToken;
                  }
              }
          }
  
  </code></pre>