Reusing the access token before it expires API side

Viewed 684

One of my spring boot application is calling 3 external APIS , before each call it is obtaining the access token for the corresponding API using REST calls. Even though these access tokens are live up to 30 minutes the client app is re generating these tokens per request.

          Is there any effective way , supported by the frame work to re use the token before it got expired?

I have custom solution which will cache the token in a volatile variable along with expiry time and every request there is a function to check whether the token expired or not , if expired it will generate a new token.

 private boolean hasExpired() {
    if (ObjectUtils.isEmpty(this.expiryTime))
        return true;
    return new Date().after(this.expiryTime);

}

Is there any other way better than this? the intention is to reduce the number calls and save time.

1 Answers
Related