【问题标题】:Since lexik/jwt-authentication-bundle 2.16: The "lexik_jwt_authentication.user_identity_field" configuration key is deprecated由于 lexik/jwt-authentication-bundle 2.16:不推荐使用“lexik_jwt_authentication.user_identity_field”配置键
【发布时间】:2022-10-14 23:48:02
【问题描述】:

我刚刚将lexik/jwt-authentication-bundlev2.16.0 升级到版本3.x-dev,因为我遇到了这个错误Fatal error Cannot declare trait ForwardCompatAuthenticatorTrait,然后升级到版本3.x-dev 修复了这个错误。

现在我有一条弃用消息:

deprecation.INFO: User Deprecated: Since lexik/jwt-authentication-bundle 2.16: The "lexik_jwt_authentication.user_identity_field" configuration key is deprecated since version 2.16, implement "Symfony\Component\Security\Core\User\UserInterface::getUserIdentifier()" instead. {"exception":"[object] (ErrorException(code: 0): User Deprecated: Since lexik/jwt-authentication-bundle 2.16: The \"lexik_jwt_authentication.user_identity_field\" configuration key is deprecated since version 2.16, implement \"Symfony\\Component\\Security\\Core\\User\\UserInterface::getUserIdentifier()\" instead. at /srv/api/vendor/symfony/config/Definition/ArrayNode.php:241)"} []

这是我的配置:

# api/config/packages/lexik_jwt_authentication.yaml
lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
    token_ttl: 604800 # 1 week
    user_identity_field: email # https://stackoverflow.com/questions/50629890/symfony-security-component-unable-to-find-key-username-in-the-token-payloa/50630492

我知道我必须删除 user_identity_field 但是当我删除它时出现错误: {"code":401,"message":"Unable to find key \u0022username\u0022 in the token payload."}username 是默认的 user_identity_field 值。

我怎样才能解决这个问题 ?

还有我的security.yml

# api/config/packages/security.yaml
security:
  # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
  password_hashers:
    Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'

  # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
  providers:
    # used to reload user from session & other features (e.g. switch_user)
    app_user_provider:
      entity:
        class: App\Entity\User

  firewalls:
    dev:
      pattern: ^/(_(profiler|wdt)|css|images|js)/
      security: false
    api:
      # https://api-platform.com/docs/core/jwt/
      # https://github.com/lexik/LexikJWTAuthenticationBundle/blob/2.x/Resources/doc/index.md#configuration
      pattern: ^/api/
      stateless: true
      provider: app_user_provider
      jwt: ~
      switch_user: { parameter: X-Switch-User }
    login:
      stateless: true
      json_login:
        check_path: /authentication-token
        success_handler: lexik_jwt_authentication.handler.authentication_success
        failure_handler: lexik_jwt_authentication.handler.authentication_failure
        
    # ...

api/src/Entity/User.php

<?php

namespace App\Entity;

use App\Repository\UserRepository;
use App\Utils\StringLengthUtils;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Table;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;

#[ORM\Entity(repositoryClass: UserRepository::class)]
#[Table(name: 'user_t')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
    #[ORM\Column(type: Types::INTEGER)]
    #[ORM\GeneratedValue]
    #[ORM\Id]
    private ?int $id = null;

    #[ORM\Column(type: Types::STRING, length: StringLengthUtils::MAX_LENGTH, unique: true)]
    private ?string $email = null;

    // other properties ...

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getUserIdentifier(): string
    {
        return $this->email;
    }

    // other getter and setters ...
}

【问题讨论】:

    标签: symfony jwt


    【解决方案1】:

    而不是user_identity_field 参数,您需要使用username_path

    login:
      stateless: true
      json_login:
        username_path: email
        check_path: /authentication-token
        success_handler: lexik_jwt_authentication.handler.authentication_success
        failure_handler: lexik_jwt_authentication.handler.authentication_failure
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2022-07-14
      • 2012-05-05
      • 2015-03-13
      • 2018-10-14
      • 1970-01-01
      相关资源
      最近更新 更多