【问题标题】:Lumen generate token without verifying username and passwordLumen生成token无需验证用户名和密码
【发布时间】:2018-09-25 08:19:51
【问题描述】:

我正在尝试通过验证其他字段和表而不是用户表的电子邮件和密码来生成令牌。我正在使用tymon jwt 库。

我有三个字段需要验证以验证用户身份

 table::where(["id"=>"1","mobile"=>"123","otp"=>"asdf"])->get();

因此,如果我在表中找到与此条件匹配的行,那么我想对用户进行身份验证并生成带有所需声明的有效令牌。

到目前为止我尝试过的是:

//after check for three fields in DB. If row matches then, $id and $contact are variable from DB.
$customClaims = ['id' => $id, 'mobile' => $contact];

$payload = JWTFactory::make($customClaims);

当我尝试这个时,我得到了JWT payload does not contain the required claims

那么如何使用三个字段对用户进行身份验证,并生成具有所需声明和$customClaims 的有效令牌。

已编辑

 public function verifyOTP(Request $request) {
    $otp = $request->otp;
    $schoolid = $request->schoolid;
    $parent_contact = $request->contactNum;
    $verifyOTP = OTP::where(['schoolid' => $schoolid, 'parent_numb' => $parent_contact, 'otp' => $otp])->get();
    if ($verifyOTP) {

        $customClaims = ['schoolid' => $schoolid, 'parent_numb' => $parent_contact];

        $payload = JWTFactory::make($customClaims);

        $token = JWTAuth::encode($payload);
        return $token;
    }
}

【问题讨论】:

  • 可以分享所有相关代码吗?
  • @C2486 请参考编辑部分的完整代码。
  • 您正在寻找的是\Tymon\JWTAuth\Facades\JWTAuth::fromUser($user, $customClaims = []),其中$user 是某种具有id 字段的实体。您在这里遇到的错误是因为给定的声明不包含必需的声明 - 'iss'、'iat'、'exp'、'nbf'、'sub'、'jti'。您可以手动填写它们或使用给定的方法。
  • @GiedriusKiršys 如果您知道解决方案,为什么不写一个答案?

标签: laravel jwt lumen tymon-jwt


【解决方案1】:

在这种情况下Crypt会变得很方便,不知道你以后要不要做任何认证。 Crypt 将帮助您加密和解密。

Make trait 或任何其他类,你不必太担心 Encrypt 和 Decrypt,Crypt 可以处理。我使用 Crypt

构建了移动 API

加密: 地穴::加密($value);

解密: crypt::decrypt($value);

更多信息:https://laravel.com/docs/5.2/encryption

【讨论】:

  • 加密和解密不仅仅是 JWT 做的事情。 JWT 需要基于该字段进行身份验证并使用服务器密钥进行验证。
  • 是的。像明智的加密解密一样,它们的生命周期取决于您要如何处理。取决于您如何创建或构建它。
  • 这甚至是上述问题的答案吗?它如何解决 JWT 中缺少声明的 OPs 问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多