laravel引入Lcobucci-JWT使用遇到的时间时区问题修改设置时区也是不管用
2. 问题描述?
Lcobucci/JWT 版本是3.4
php 7.4
nginx
mac pro
– 创建token设置时区
// 生成token
public function getToken($uid){
$signer = new Sha256();
$now = new DateTimeImmutable(getDateTime(time()),new DateTimeZone("Asia/Shanghai"));
$token = (new Builder())
->issuedBy('yabang-com')
->issuedAt($now)
// ->canOnlyBeUsedAfter($now)
->expiresAt($now->modify("+3 hour"))
->withClaim('uid',$uid)
->sign($signer,'yabang')
->getToken();
return $token->toString();
}
– 解析出的结果时间还是不对
Lcobucci\JWT\Token\DataSet Object
(
[data:Lcobucci\JWT\Token\DataSet:private] => Array
(
[iss] => yabang-com
[iat] => DateTimeImmutable Object
(
[date] => 2022-12-05 06:06:53.000000
[timezone_type] => 1
[timezone] => +00:00
)
[exp] => DateTimeImmutable Object
(
[date] => 2022-12-05 09:06:53.000000
[timezone_type] => 1
[timezone] => +00:00
)
[uid] => 2
)
[encoded:Lcobucci\JWT\Token\DataSet:private] => eyJpc3MiOiJ5YWJhbmctY29tIiwiaWF0IjoxNjcwMjIwNDEzLCJleHAiOjE2NzAyMzEyMTMsInVpZCI6Mn0
)
求大神指点一二
laravel 8 框架配置的时区是 Asia/Shanghai
use Lcobucci\JWT\Token\Parser;
use Lcobucci\Clock\SystemClock;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Validation\Validator;
use Lcobucci\JWT\Validation\Constraint\StrictValidAt;
$jwt = ‘xxxxx’;
$parser = new Parser(new JoseEncoder());
$token = $parser->parse($jwt);
//获取值:
$iat = $token->claims()->get(‘iat’); // 发布时间
$iat = $iat->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$exp = $token->claims()->get(‘exp’); // 有效期
$exp = $iat->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$exp = $token->claims()->get(‘nbf’); // 生效时间
$exp = $iat->setTimezone(new \DateTimeZone(date_default_timezone_get()));
//验证值:
$validator = new Validator();
//验证jwt时效是否过期 前提你设置了 iat,exp,nbf
$validator->assert($token ,new StrictValidAt(SystemClock::fromSystemTimezone()));