I see the below restTemplate being used for fetching OAuth token. I don't see any explicit call to cache the token in my application. However I see the same token value being returned by this template. Does OAuth2RestTemplate inherently implement client side caching? If so , does it make an explicit call to the OAuth2 endpoint when the token expires?
@Qualifier("oauth")
@Bean
public OAuth2RestTemplate restTemplate(@Qualifier("resourceDetails") ClientCredentialsResourceDetails resourceDetails) {
return new OAuth2RestTemplate(resourceDetails);
}
I saw the below in the documentation -
getAccessToken public OAuth2AccessToken getAccessToken() throws UserRedirectRequiredException
Acquire or renew an access token for the current context if necessary. This method will be called automatically when a request is executed (and the result is cached), but can also be called as a standalone method to pre-populate the token.
it's clearly said that the token is cached in the context (DefaultOAuth2ClientContext object) created when defining a resource. however, the token is managed by calling getAccessToken(). here is what the Docs says about getAccessToken():
Acquire or renew an access token for the current context if necessary. This method will be called automatically * when a request is executed (and the result is cached), but can also be called as a standalone method to * pre-populate the token.
the token is cached until it's Expired, then it will be renewed automatically. this snippet of code from getAccessToken() describes it:
if (accessToken == null || accessToken.isExpired()) {
try {
accessToken = acquireAccessToken(context);
}
About Caching mechanism, By default spring provides an in-memory caching
DefaultOAuth2ClientContext
but you can provide your own implementation of
OAuth2ClientContext
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With