更新jwt问题
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user