Skip to content

Commit 19d47c8

Browse files
houaiaiclaude
andcommitted
feat: 优化leeway时间处理机制,解决Redis缓存与JWT验证不一致问题
- 在RedisHandler中添加leeway时间支持,确保Redis缓存TTL包含leeway时间 - 修改generateToken和refreshToken方法,使用calculateFinalTtl计算最终TTL - 新增getJwtConfig方法获取JWT配置,包含leeway设置 - 移除测试环境相关配置,简化代码逻辑 - 解决issue #39中提到的access token过期后在leeway时间内仍然有效的问题 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e7bcf91 commit 19d47c8

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/JwtToken.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,6 @@ private static function getPrivateKey(array $config, int $tokenType = self::ACCE
386386
*/
387387
private static function _getConfig(): array
388388
{
389-
// 在测试环境中,直接加载配置文件
390-
if (defined('PHPUNIT_RUNNING') || getenv('APP_ENV') === 'testing') {
391-
$configFile = __DIR__ . '/config/plugin/tinywan/jwt/app.php';
392-
if (file_exists($configFile)) {
393-
$config = require $configFile;
394-
return $config['jwt'] ?? [];
395-
}
396-
}
397-
398389
$config = config('plugin.tinywan.jwt.app.jwt');
399390
if (empty($config)) {
400391
throw new JwtConfigException('jwt配置文件不存在');

src/RedisHandler.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private static function checkConnection()
3939
* @param string $uid 用户ID
4040
* @return string 完整的缓存键名
4141
*/
42-
private static function generateKey(string $pre, string $client, string $uid)
42+
private static function generateKey(string $pre, string $client, string $uid): string
4343
{
4444
return sprintf('%s%s:%s', $pre, $client, $uid);
4545
}
@@ -79,7 +79,10 @@ public static function generateToken(string $pre, string $client, string $uid, i
7979

8080
self::safeExecute(function() use ($cacheKey, $ttl, $token) {
8181
Redis::del($cacheKey);
82-
$result = Redis::setex($cacheKey, $ttl, $token);
82+
83+
// 增加leeway时间到TTL中,确保在宽容时间内Redis缓存仍然存在
84+
$finalTtl = self::calculateFinalTtl($ttl);
85+
$result = Redis::setex($cacheKey, $finalTtl, $token);
8386

8487
if (!$result) {
8588
throw new RedisConnectionException('Redis设置令牌失败');
@@ -112,7 +115,9 @@ public static function refreshToken(string $pre, string $client, string $uid, in
112115
}
113116
}
114117

115-
$result = Redis::setex($cacheKey, $ttl, $token);
118+
// 增加leeway时间到TTL中,确保在宽容时间内Redis缓存仍然存在
119+
$finalTtl = self::calculateFinalTtl($ttl);
120+
$result = Redis::setex($cacheKey, $finalTtl, $token);
116121
if (!$result) {
117122
throw new RedisConnectionException('Redis刷新令牌失败');
118123
}
@@ -195,17 +200,46 @@ private static function validateRedisParams(int $ttl, string $token): void
195200
}
196201
}
197202

203+
/**
204+
* @desc: 计算包含leeway时间的最终TTL
205+
* @param int $ttl 原始TTL
206+
* @return int 包含leeway时间的最终TTL
207+
*/
208+
private static function calculateFinalTtl(int $ttl): int
209+
{
210+
// 获取JWT配置中的leeway时间
211+
$config = self::getJwtConfig();
212+
$leeway = $config['leeway'] ?? 0;
213+
214+
// 返回原始TTL加上leeway时间
215+
return $ttl + $leeway;
216+
}
217+
218+
/**
219+
* @desc: 获取JWT配置
220+
* @return array JWT配置数组
221+
*/
222+
private static function getJwtConfig(): array
223+
{
224+
if (function_exists('config')) {
225+
$config = config('plugin.tinywan.jwt.app.jwt');
226+
if (!empty($config)) {
227+
return $config;
228+
}
229+
}
230+
231+
// 默认配置
232+
return [
233+
'leeway' => 60,
234+
];
235+
}
236+
198237
/**
199238
* @desc: 检查Redis是否可用
200239
* @return bool Redis是否可用
201240
*/
202241
public static function isAvailable(): bool
203242
{
204-
// 检查是否在测试环境
205-
if (defined('PHPUNIT_RUNNING') || getenv('APP_ENV') === 'testing') {
206-
return false;
207-
}
208-
209243
// 检查Redis类是否可用
210244
if (!class_exists('support\Redis')) {
211245
return false;

0 commit comments

Comments
 (0)