Class DefaultTokenManager
- All Implemented Interfaces:
TokenManager, AutoCloseable
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classDefaultTokenManager Builder DefaultTokenManager 构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultTokenManager.Builderbuilder()Create a new builder.voidclear()Clear all tokens 清除所有 Tokenvoidclose()Close the token manager and release resources 关闭 Token 管理器并释放资源booleanCheck if a token exists for the key 检查是否存在该键的 TokenGet a stored token 获取存储的 TokengetOrObtain(String key, Supplier<OAuth2Token> tokenSupplier) Get a valid token or obtain a new one 获取有效的 Token 或获取新的getValidToken(String key) Get a valid token, refreshing if necessary 获取有效的 Token,必要时刷新booleanhasValidToken(String key) Check if a valid token exists for the key 检查是否存在该键的有效 TokenvoidRemove a token 移除 Tokenintsize()Get the number of stored tokens 获取存储的 Token 数量voidstore(String key, OAuth2Token token) Store a token 存储 Token
-
Method Details
-
store
Store a token 存储 Token- Specified by:
storein interfaceTokenManager- Parameters:
key- the storage key (e.g., user ID) | 存储键(例如用户 ID)token- the token to store | 要存储的 Token
-
get
Get a stored token 获取存储的 Token- Specified by:
getin interfaceTokenManager- Parameters:
key- the storage key | 存储键- Returns:
- the token if found | 找到的 Token
-
getValidToken
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:
getValidTokenin interfaceTokenManager- Parameters:
key- the storage key | 存储键- Returns:
- the valid token | 有效的 Token
-
getOrObtain
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:
getOrObtainin interfaceTokenManager- Parameters:
key- the storage key | 存储键tokenSupplier- supplier to obtain new token if needed | 需要时获取新 Token 的提供者- Returns:
- the valid token | 有效的 Token
-
exists
Check if a token exists for the key 检查是否存在该键的 Token- Specified by:
existsin interfaceTokenManager- Parameters:
key- the storage key | 存储键- Returns:
- true if token exists | 如果存在 Token 返回 true
-
hasValidToken
Check if a valid token exists for the key 检查是否存在该键的有效 Token- Specified by:
hasValidTokenin interfaceTokenManager- Parameters:
key- the storage key | 存储键- Returns:
- true if valid token exists | 如果存在有效 Token 返回 true
-
remove
Remove a token 移除 Token- Specified by:
removein interfaceTokenManager- Parameters:
key- the storage key | 存储键
-
clear
-
size
public int size()Get the number of stored tokens 获取存储的 Token 数量- Specified by:
sizein interfaceTokenManager- 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceTokenManager
-
builder
Create a new builder. 创建新的构建器。- Returns:
- the builder | 构建器
-