微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

lexik/LexikJWTAuthenticationBundle 不适用于 symfony 5.2

如何解决lexik/LexikJWTAuthenticationBundle 不适用于 symfony 5.2

我正在尝试在 symfony 5.2 项目中安装和配置 lexik/LexikJWTAuthenticationBundle。 当我应用以下文档 https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/index.md#installation 时,它给了我这个错误

您必须配置“public_key”或“secret_key”。

所以,我应用了另一个配置:

lexik_jwt_authentication:
  secret_key: '%env(resolve:JWT_SECRET_KEY)%'
  public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
  pass_phrase: '%env(JWT_PAsspHRASE)%'
  token_ttl: '%env(JWT_TTL)%'

在我的 .env 文件中,我有这个:

###> lexik/jwt-authentication-bundle ###
JWT_SECRET_KEY=%kernel.project_dir%/config/jwt/private.pem
JWT_PUBLIC_KEY=%kernel.project_dir%/config/jwt/public.pem
JWT_PAsspHRASE=mypassphrase
JWT_TTL=3600 # 1 hour expiration
###< lexik/jwt-authentication-bundle ###

然后我运行这些命令来生成密钥(在我的目录 config/jwt 下成功生成了密钥):

- openssl genpkey -out config/jwt/private.pem -aes256 -algorithm rsa -pkeyopt rsa_keygen_bits:4096
- openssl pkey -in config/jwt/private.pem -out config/jwt/public.pem -pubout

但是,当我运行此命令以检查它是否有效时,出现此错误

curl -X POST -H "Content-Type: application/json" http://localhost:8000/api -d '{"username":"johndoe","password":"test"}'

"发生错误","status":400,"detail":"无效 JSON.","class":"Symfony\Component\HttpKernel\Exception\BadRequestHttpException

当我在控制器中测试此方法时出现此错误,如下所述:https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/7-manual-token-creation.md :

"不能自动装配参数 $user of “App\Controller\DefaultController::getTokenUser()”:它引用 界面“Symfony\Component\Security\Core\User\UserInterface” 但不存在这样的服务。您是否创建了一个实现的类 这个界面?”

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;

class ApiController extends Controller
{
    public function getTokenUser(UserInterface $user,JWTTokenManagerInterface $JWTManager)
    {
        // ...

        return new JsonResponse(['token' => $JWTManager->create($user)]);
    }
}

这个包是否与 Symfony 5.2 兼容,我该如何解决这个问题?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。