【问题标题】:How to create a JWT Token without authentication如何在没有身份验证的情况下创建 JWT 令牌
【发布时间】:2019-05-22 23:59:25
【问题描述】:
  • “laravel/framework”:“5.7.*”
  • "tymon/jwt-auth": "dev-develop"

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

  • 使用 Tymon/JWTAuth (https://github.com/tymondesigns/jwt-auth) 由于最新的 Laravel 存在问题,建议加载最新的开发 ( 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

我希望收到包含上述有效负载的令牌。

【问题讨论】:

  • 您好,也许是 jwt-auth 包不允许您在没有身份验证声明的情况下使用 jwt 令牌?也许你应该尝试在这里使用原生 jwt 库 jwt.io/#libraries-io ?您也可以尝试覆盖生成令牌的方法

标签: php laravel jwt


【解决方案1】:

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

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

还要确保您已运行 php artisan jwt:secret 或以其他方式提供用于签名的密钥。

【讨论】:

    【解决方案2】:

    我通过在config/jwt.php 中注释掉required_claims 中的'exp' 解决了这个问题:

    .....
    'required_claims' => [
       'iss',
       'iat',
       //'exp',
    .....
    ],
    

    【讨论】:

      猜你喜欢
      • 2019-08-09
      • 2019-05-10
      • 1970-01-01
      • 2014-08-09
      • 2018-02-23
      • 2020-05-05
      • 1970-01-01
      • 2020-09-06
      • 2020-05-12
      相关资源
      最近更新 更多