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

jwt-auth 配置文件

file
file

如果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 协议》,转载必须注明作者和本文链接
laravel_lost
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 1

有时新部署项目,忘记配置TTL,,结果就是,一登录,就提示token已过期,,,

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!