From 20381e7a5f1b87003ae08f7c44431440435191fd Mon Sep 17 00:00:00 2001 From: "453530270@qq.com" Date: Tue, 8 Oct 2024 11:39:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=BD=E8=BE=93=E5=87=BA=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E7=9A=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Index.php | 42 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/app/api/controller/Index.php b/app/api/controller/Index.php index 6b5d993..2d6769e 100644 --- a/app/api/controller/Index.php +++ b/app/api/controller/Index.php @@ -57,26 +57,32 @@ class Index extends BaseController $signer = new Sha256(); $key = InMemory::plainText(config('jwt.secret')); - $builder = new Builder(); - // 设置发行时间和过期时间 + // + $config = Configuration::forSymmetricSigner($signer,$key); $now = new DateTimeImmutable(); // 当前时间 - // 设置发行时间和过期时间 - $secondsToAdd = (int) config('jwt.token_ttl'); - $expiresAt = $now->add(new \DateInterval('PT' . $secondsToAdd . 'S')); + $token = $config->builder() + // 签发人 + ->issuedBy('https://douyin.xingtongworld.com/') + // 受众 + ->permittedFor('https://douyin.xingtongworld.com/') + // JWT ID 编号 唯一标识 + ->identifiedBy($claims['id']) + // 签发时间 + ->issuedAt($now) + // 在1分钟后才可使用 +// ->canOnlyBeUsedAfter($now->modify('+1 minute')) + // 过期时间1小时 + ->expiresAt($now->modify('+1 hour')) + // 自定义uid 额外参数 + ->withClaim('uid', $claims['id']) + ->withClaim('name',$claims['nick_name']) + // 自定义header 参数 + // ->withHeader('foo', 'bar') + // 生成token + ->getToken($config->signer(), $config->signingKey()); - $token = $builder - ->issuedAt($now) // iat: 发行时间 - ->expiresAt($expiresAt) // exp: 过期时间 - ->withIssuer('iss', 'xtt') // iss: 发行人 - ->withSubject('sub', 'xtoken') // sub: 主题 - ->withAudience('aud', 'ttc'); // aud: 受众 - - - foreach ($claims as $key => $value) { - $token = $token->withClaim($key, $value); - } - - return (string) $token->sign($signer, $key); + // base64 + return $token->toString(); } } \ No newline at end of file