Skip to content

Commit

Permalink
Merge pull request #117 from mineadmin/2.0
Browse files Browse the repository at this point in the history
2.0
  • Loading branch information
zds-s authored Feb 1, 2024
2 parents 54688e5 + 0a91c23 commit 4faf4be
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/System/Service/SystemUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ public function getOnlineUserPageList(array $params = []): array
{
$redis = redis();
$key = sprintf('%sToken:*', config('cache.default.prefix'));
$jwt = $this->container->get(JWT::class);
$blackList = $this->container->get(JWT::class)->blackList;
$userIds = [];
$iterator = null;

while (false !== ($users = $redis->scan($iterator, $key, 100))) {
foreach ($users as $user) {
// 如果是已经加入到黑名单的就代表不是登录状态了
if (! $blackList->hasTokenBlack($user) && preg_match("/{$key}(\\d+)$/", $user, $match) && isset($match[1])) {
if (! $this->hasTokenBlack($redis->get($user)) && preg_match("/{$key}(\\d+)$/", $user, $match) && isset($match[1])) {
$userIds[] = $match[1];
}
}
Expand Down Expand Up @@ -339,4 +340,20 @@ protected function handleData(array $data): array
}
return $data;
}

private function hasTokenBlack(string $token): bool
{
$jwt = $this->container->get(JWT::class);
$scenes = array_keys(config('jwt.scene'));
foreach ($scenes as $scene) {
$sceneJwt = $jwt->setScene($scene);
if ($sceneJwt->blackList->hasTokenBlack(
$sceneJwt->getParserData($token),
$jwt->getSceneConfig($scene)
)) {
return true;
}
}
return false;
}
}
1 change: 1 addition & 0 deletions config/autoload/jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
'sso_key' => 'id',
'ttl' => 7200, // token过期时间,单位为秒
'blacklist_cache_ttl' => env('JWT_TTL', 7200), // 黑名单缓存token时间,注意:该时间一定要设置比token过期时间要大一点,默认为100秒,最好设置跟过期时间一样
'blacklist_enabled' => true,
],
'application2' => [
'secret' => 'application2', // 非对称加密使用字符串,请使用自己加密的字符串
Expand Down

0 comments on commit 4faf4be

Please sign in to comment.