|
|
|
@ -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(); |
|
|
|
} |
|
|
|
} |