【问题标题】:Reusing the access token before it expires API side在 API 端过期之前重用访问令牌
【发布时间】:2025-12-15 10:00:02
【问题描述】:

我的一个 Spring Boot 应用程序正在调用 3 个外部 APIS,在每次调用之前,它都使用 REST 调用获取相应 API 的访问令牌。即使这些访问令牌的有效期长达 30 分钟,客户端应用程序也会根据请求重新生成这些令牌。

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

我有自定义解决方案,它将令牌与到期时间一起缓存在 volatile 变量中,并且每个请求都有一个函数来检查令牌是否过期,如果过期它将生成一个新令牌。

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

}

还有比这更好的方法吗?目的是减少通话次数并节省时间。

【问题讨论】:

    标签: spring spring-boot spring-security-oauth2


    【解决方案1】:

    使用 spring 安全框架的 OAuth2AuthorizedClientManager 。如果需要,这将负责刷新令牌。

    【讨论】: