更新jwt问题

This commit is contained in:
2026-01-26 14:48:27 +08:00
parent dbb40c38c4
commit 1a7303cedf
6 changed files with 142 additions and 4 deletions
+32 -2
View File
@@ -7,9 +7,9 @@ use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\Request;
use think\facade\Session;
use think\facade\Cache;
use app\admin\controller\OperationLog\OperationLogger;
use app\service\JwtService;
/**
* 控制器基础类
@@ -159,7 +159,37 @@ abstract class BaseController
*/
protected function getAdminUserInfo(): array
{
return JwtService::getUserFromHeader($this->request->header('Authorization', ''));
$userId = $this->request->header('Authorization', '');
if (!preg_match('/Bearer\s+(.+)/i', $userId, $matches)) {
return ['id' => 0, 'account' => '', 'name' => ''];
}
$tokenData = $this->verifyTokenFromHeader($matches[1]);
if (!$tokenData || !isset($tokenData['user'])) {
return ['id' => 0, 'account' => '', 'name' => ''];
}
return (array)$tokenData['user'];
}
private function verifyTokenFromHeader($token): ?array
{
$authHeader = $this->request->header('Authorization', '');
if (!preg_match('/Bearer\s+(.+)/i', $authHeader, $matches)) {
return null;
}
try {
$decoded = \Firebase\JWT\JWT::decode(
$matches[1],
new \Firebase\JWT\Key(\app\service\JwtService::getSecret(), 'HS256')
);
return (array)$decoded;
} catch (\Exception $e) {
return null;
}
}
}