简体   繁体   English

Jwt 身份验证错误参数 3 传递给 Lcobucci\JWT\Signer\Hmac::doVerify()

[英]Jwt Authentication error Argument 3 passed to Lcobucci\JWT\Signer\Hmac::doVerify()

I'm working on a project to learn laravel, I saw a couple of tutorials to use jwt auth in this.我正在研究一个学习 laravel 的项目,我看到了一些使用jwt auth的教程。 It was working good but now is show an error not always but I don't know why.它工作得很好,但现在并不总是显示错误,但我不知道为什么。 this is it:就是这个:

Argument 3 passed to Lcobucci\JWT\Signer\Hmac::doVerify() must be an instance of Lcobucci\JWT\Signer\Key, null given, called in C:\xampp\htdocs\inmobiliaria\vendor\lcobucci\jwt\src\Signer\BaseSigner.php on line 42传递给 Lcobucci\JWT\Signer\Hmac::doVerify() 的参数 3 必须是 Lcobucci\JWT\Signer\Key 的一个实例,null 给定,在 C:\xampp\htdocs\inmobiliaria\ven 中调用\Signer\BaseSigner.php 在第 42 行

Sometimes it works but not another.有时它有效,但另一种无效。 so I don't know what can I do.所以我不知道我能做什么。 I tried with我试过了

  1. php artisan cache:clear php 工匠缓存:清除
  2. php artisan config:clear php 工匠配置:清除
  3. php artisan jwt:secret php 工匠 jwt:秘密

but the problem still there, sometimes i get good answer from server(200) but the most part of then al bad (500).但问题仍然存在,有时我从服务器(200)得到很好的答案,但大部分时间都不好(500)。

Screenshots截图

在此处输入图像描述

在此处输入图像描述

Here's complete snippet这是完整的片段

php artisan key:generate
php artisan jwt:secret
php artisan cache:clear
php artisan config:clear

Before run above commands make sure there is install jwt package, than check its config file name jwt.php in config folder.在运行上述命令之前,请确保安装了 jwt 包, jwt.php在 config 文件夹中检查其配置文件名jwt.php

In fact it's the right thing to solve it:事实上,解决它是正确的:

php artisan key:generate
php artisan jwt:secret

I came across this error when I deployed my application after testing and everything else worked well on the local server.我在测试后部署我的应用程序时遇到了这个错误,其他一切都在本地服务器上运行良好。

I noticed the following;我注意到以下几点;

  • It's possible that you are getting this error when you uploaded the applications before implementing the JWT and re-uploaded directly via the Cpanel upload without reuploading the .env file.当您在实施 JWT 之前上传应用程序并直接通过 Cpanel 上传重新上传而不重新上传 .env 文件时,您可能会收到此错误。

    • if this is the case, the simplest way to solve this is my merely adding the JWT:key in your .env file (at the bottom) ef如果是这种情况,解决这个问题的最简单方法就是在 .env 文件(底部)中添加 JWT:key ef

JWT_SECRET=roCZauuldMpw5i4039393939393372y98bEWumqd9ls7Uk8DEpr0gIZ6WIWB JWT_SECRET=roCZauuldMpw5i4039393939393372y98bEWumqd9ls7Uk8DEpr0gIZ6WIWB

Hope this helps :)希望这可以帮助 :)

This error can occur when这个错误可能发生在

  • JWT_SECRET is missing on the .env JWT_SECRET中缺少.env
  • JWT_SECRET is commented in the .env JWT_SECRET在评论.env
  • The value of JWT_SECRET is wrong. JWT_SECRET的值是错误的。

In my case, running php artisan jwt:secret it was appending the key on the current JWT_SECRET .在我的情况下,运行php artisan jwt:secret它是在当前JWT_SECRET上附加密钥。 So checking my .env file, the value of JWT_SECRET was very long and wrong (it should be 64 chars long).所以,检查我的.env文件,价值JWT_SECRET很长,错误(应该是64个字符长)。

To solve it:要解决它:

  1. Open the .env and remove the JWT_SECRET value (note that you have to remove it and not comment it).打开.env并删除JWT_SECRET值(请注意,您必须删除它而不是注释它)。
  2. Run php artisan jwt:secret运行php artisan jwt:secret

I have resolved the same on Hostinger - Shared Hosting in a bit different manner than others explained.我在 Hostinger - Shared Hosting 上解决了同样的问题,但与其他人解释的方式略有不同。 On shared hosting's user will not have access to shell/cmd/ssh access to the server - So I have executed the commands using composer - Thanks to Ángel Carlos del Pozo Muela for sharing the idea in https://stackoverflow.com/a/48682668 for a different solution but it worked in my case here as well在共享主机上,用户将无法访问服务器的 shell/cmd/ssh 访问权限 - 所以我已经使用 composer 执行了命令 - 感谢 Ángel Carlos del Pozo Muela 在https://stackoverflow.com/a/ 中分享这个想法48682668用于不同的解决方案,但它也适用于我的情况

I have updated the composer.json >> script tag with extra values我已经用额外的值更新了 composer.json >> 脚本标签

"scripts": {
    "pre-generate-key": [
        "@php artisan key:generate",
         "@php artisan cache:clear",
        "@php artisan config:clear"
    ],"post-autoload-dump": [
        "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
        "@php artisan package:discover --ansi"
    ]

Followed by that I have created a new parameter in .env file using going to hostinger file manager and deployed the changes.接着我使用转到托管文件管理器在 .env 文件中创建了一个新参数并部署了更改。

 JWT_SECRET={{value}}

on successful completion of deployment - Tested the all service -> all services started working fine - then I removed the "pre-generate-key" bit from composer.json and re-deployed code成功完成部署后 - 测试了所有服务 -> 所有服务开始工作正常 - 然后我从 composer.json 中删除了“预生成密钥”位并重新部署了代码

php artisan jwt:secret
php artisan config:cache

These both command will be enough这两个命令就足够了

我正在应用程序平台上工作 - Digital Ocean php artisan jwt:secret为我做 -> 它只是生成一个新的秘密,这就是搞砸的(只需检查你的 .env 文件)

This can also happen when using the Laravel Vapor service if you haven't remembered to add the JWT_SECRET to the env file used by Vapor.如果您不记得将JWT_SECRET添加到JWT_SECRET使用的env文件,则在使用 Laravel Vapor 服务时也会发生这种情况。

In that case just pull the .env file down for the current environment (in this case staging ):在这种情况下,只需为当前环境拉下.env文件(在这种情况下staging ):

vapor env:pull staging

and add the JWT_SECRECT line to it, followed by a push:并向其中添加JWT_SECRECT行,然后推送:

vapor env:push staging

and re-deploy.并重新部署。

I had this problem with my Laravel 8.x and PHP 7.4 so I solved the problem by updating my keys just with these commands:我的Laravel 8.xPHP 7.4遇到了这个问题,所以我通过使用以下命令更新我的密钥解决了这个问题:

php artisan key:generate
php artisan jwt:secret

Problem Statement :问题陈述

TypeError: Argument 3 passed to Lcobucci\JWT\Signer\Hmac::doVerify() m
Facing above problem

Larave version:    8.77
Php Versionn: 7.4

Solution works 100%解决方案 100% 有效

php artisan key:generate
php artisan jwt:secret
php artisan cache:clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM