Class DefaultTokenManager

java.lang.Object
cloud.opencode.base.oauth2.token.DefaultTokenManager
All Implemented Interfaces:
TokenManager, AutoCloseable

public final class DefaultTokenManager extends Object implements TokenManager
Default Token Manager Implementation 默认 Token 管理器实现

Thread-safe implementation of TokenManager that delegates to TokenStore for persistence and TokenRefresher for refresh operations. Uses per-key locking to prevent thundering herd during token refresh.

线程安全的 TokenManager 实现,委托给 TokenStore 进行持久化, 委托给 TokenRefresher 进行刷新操作。 使用按键锁定防止 Token 刷新时的惊群效应。

Features | 主要功能:

  • Thread-safe token storage and retrieval - 线程安全的 Token 存储和检索
  • Automatic token refresh with de-duplication - 带去重的自动 Token 刷新
  • Per-key locking to prevent thundering herd - 按键锁定防止惊群
  • Builder pattern for construction - 构建器模式用于构建
  • Resource lifecycle management - 资源生命周期管理

Usage Examples | 使用示例:

// Build a token manager
DefaultTokenManager manager = DefaultTokenManager.builder()
    .tokenStore(new InMemoryTokenStore())
    .refresher(refresher)
    .refreshThreshold(Duration.ofMinutes(5))
    .build();

// Store and retrieve tokens
manager.store("user-1", token);
OAuth2Token validToken = manager.getValidToken("user-1");

// Get or obtain
OAuth2Token token = manager.getOrObtain("user-1", () -> obtainNewToken());

// Clean up
manager.close();

Thread Safety | 线程安全:

This class is thread-safe. Concurrent requests for the same key will share a single refresh operation rather than triggering multiple refreshes.

此类是线程安全的。对同一键的并发请求将共享单个刷新操作, 而不是触发多次刷新。

Since:
JDK 25, opencode-base-oauth2 V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • store

      public void store(String key, OAuth2Token token)
      Store a token 存储 Token
      Specified by:
      store in interface TokenManager
      Parameters:
      key - the storage key (e.g., user ID) | 存储键(例如用户 ID)
      token - the token to store | 要存储的 Token
    • get

      public Optional<OAuth2Token> get(String key)
      Get a stored token 获取存储的 Token
      Specified by:
      get in interface TokenManager
      Parameters:
      key - the storage key | 存储键
      Returns:
      the token if found | 找到的 Token
    • getValidToken

      public OAuth2Token getValidToken(String key)
      Get a valid token, refreshing if necessary 获取有效的 Token,必要时刷新

      This method will:

      • Return the stored token if it's still valid
      • Refresh the token if it's expiring soon and has a refresh token
      • Throw an exception if the token is expired and cannot be refreshed

      Ensures only one refresh happens per key. Concurrent requests for the same key will wait for the in-progress refresh to complete rather than starting a new one.

      确保每个键只发生一次刷新。对同一键的并发请求将等待正在进行的刷新完成, 而不是启动新的刷新。

      Specified by:
      getValidToken in interface TokenManager
      Parameters:
      key - the storage key | 存储键
      Returns:
      the valid token | 有效的 Token
    • getOrObtain

      public OAuth2Token getOrObtain(String key, Supplier<OAuth2Token> tokenSupplier)
      Get a valid token or obtain a new one 获取有效的 Token 或获取新的

      If no token exists for the key, the supplier is called to obtain a new token.

      Specified by:
      getOrObtain in interface TokenManager
      Parameters:
      key - the storage key | 存储键
      tokenSupplier - supplier to obtain new token if needed | 需要时获取新 Token 的提供者
      Returns:
      the valid token | 有效的 Token
    • exists

      public boolean exists(String key)
      Check if a token exists for the key 检查是否存在该键的 Token
      Specified by:
      exists in interface TokenManager
      Parameters:
      key - the storage key | 存储键
      Returns:
      true if token exists | 如果存在 Token 返回 true
    • hasValidToken

      public boolean hasValidToken(String key)
      Check if a valid token exists for the key 检查是否存在该键的有效 Token
      Specified by:
      hasValidToken in interface TokenManager
      Parameters:
      key - the storage key | 存储键
      Returns:
      true if valid token exists | 如果存在有效 Token 返回 true
    • remove

      public void remove(String key)
      Remove a token 移除 Token
      Specified by:
      remove in interface TokenManager
      Parameters:
      key - the storage key | 存储键
    • clear

      public void clear()
      Clear all tokens 清除所有 Token
      Specified by:
      clear in interface TokenManager
    • size

      public int size()
      Get the number of stored tokens 获取存储的 Token 数量
      Specified by:
      size in interface TokenManager
      Returns:
      the token count | Token 数量
    • close

      public void close()
      Close the token manager and release resources 关闭 Token 管理器并释放资源

      Closes the TokenRefresher if it was created internally by this manager.

      如果 TokenRefresher 是由此管理器内部创建的,则关闭它。

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface TokenManager
    • builder

      public static DefaultTokenManager.Builder builder()
      Create a new builder. 创建新的构建器。
      Returns:
      the builder | 构建器