如何在不进行身份验证的情况下创建JWT令牌

时间:2018-12-23 10:52:27

标签: php laravel jwt

  • “ laravel / framework”:“ 5.7。*”
  • “ tymon / jwt-auth”:“ dev-develop”

我正在尝试创建具有添加的自定义声明的JWT令牌,不使用 auth(这意味着我不希望从凭据中创建令牌。)这是为了创建令牌,不需要登录,例如忘记密码/重置密码等。

  • 使用Tymon / JWTAuth(https://github.com/tymondesigns/jwt-auth) 由于Latest Laravel存在问题,因此建议加载最新的dev(1.0.x-dev)。 我尝试了以下代码但无济于事:
  • class OTL extends Model implements JWTSubject
  • use JWTAuth;
  • use Tymon\JWTAuth\Contracts\JWTSubject;
  • use Tymon\JWTAuth\Facades\JWTFactory;

public static function getJwtToken($customerId, $action, $token){ $customClaims = ['action' => $action, 'customer-id' => $customerId, 'token' => $token]; $factory = JWTFactory::customClaims($customClaims); $payload = $factory->make(); $token = JWTAuth::encode($payload); return $token;

我收到一个错误:JWT payload does not contain the required claims

我希望收到保存上面有效载荷的令牌。

2 个答案:

答案 0 :(得分:0)

我刚刚遇到了相同的错误,并且能够通过更新生成的配置文件config/jwt.php中的必需声明列表来解决该问题。

...
'required_claims' => [
    'exp',
    'sub',
    ...
],
...

还要确保您已运行php artisan jwt:secret或提供了用于签名的秘密密钥。

答案 1 :(得分:0)

我通过在'exp'的{​​{1}}中注释了required_claims来解决了这个问题:

config/jwt.php
相关问题