php-jwt 过期,jwt-auth ttl 过期设置为 null 的坑

本文详细解析了JWT认证中配置文件ttl设置为null时产生的问题及解决方案,避免因exp字段缺失导致token无法生成的情况。

jwt-auth 配置文件

ce5165863fba3f7ce0967c3dd022eb61.png

325072ce033aab514a610d9b6ed54644.png

如果ttl 的过期时间设置为null的话

请将下面的required_claims 数组中的exp 这个过期必要字段注释掉。

因为在检查比对的时候 如果ttl 设置为null 程序会把exp字段从default claim选项中删除,导致token永远产生不出来

但是会报一个异常 ,如果有写异常检查的话很容易发现,如果没有下异常处理,那只能像我一样,把代码翻一遍了。

定位文件位置 项目/vendor/tymom/jwt-auth/src/Factory.php Line 143

异常提示信息:JWT payload does not contain the required claims。

/**

* Build the default claims.

*

* @return $this

*/

protected function buildClaims()

{

// remove the exp claim if it exists and the ttl is null

if ($this->claimFactory->getTTL() === null && $key = array_search('exp', $this->defaultClaims)) {

unset($this->defaultClaims[$key]);

}

// add the default claims

foreach ($this->defaultClaims as $claim) {

$this->addClaim($claim, $this->claimFactory->make($claim));

}

// add custom claims on top, allowing them to overwrite defaults

return $this->addClaims($this->getCustomClaims());

}

所以为了测试的话,尽量设置个大点的ttl过期时间,设置null的话也无可厚非,但是有些坑还是会踩的。

本作品采用《CC 协议》,转载必须注明作者和本文链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值