JWT Symfony 6无法读取类“App\Entity\User”中的属性“username”

jei2mxaa  于 2023-05-01  发布在  其他
关注(0)|答案(3)|浏览(128)

当我实现JWT时有一个错误:

我的保安。yml:

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#loading-the-user-the-user-provider
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            lazy: true
            provider: app_user_provider
            json_login:
                check_path: api_login
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
            jwt: ~

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#the-firewall

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ~/api/login, roles: PUBLIC_ACCESS }
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

when@test:
    security:
        password_hashers:
            # By default, password hashers are resource intensive and take time. This is
            # important to generate secure password hashes. In tests however, secure hashes
            # are not important, waste resources and increase test times. The following
            # reduces the work factor to the lowest possible values.
            Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface:
                algorithm: auto
                cost: 4 # Lowest possible value for bcrypt
                time_cost: 3 # Lowest possible value for argon
                memory_cost: 10 # Lowest possible value for argon

我有当前的错误(不能得到一种方法来读取属性“用户名”在类“应用\实体\用户”)当输入正确的信用,但当我输入无效的信用我有正确的错误关于无效的信用.我做错了什么?

gab6jxml

gab6jxml1#

你的安全。yml,将property: email更改为property: username

zvms9eto

zvms9eto2#

问题出在您的安全性内的提供程序属性上。yaml文件,只需更改:

app_user_provider:
 entity:
   class: App\Entity\User
   property: email

app_user_provider:
   entity:
     class: App\Entity\User
     property: username

编辑:

我在Symfony 6上遇到了同样的问题。08,6.09和6.1、只需在用户实体中添加此功能:

public function getUsername(): string
    {
        return (string) $this->email;
    }
7vhp5slm

7vhp5slm3#

config/packages/lexik_jwt_authentication。亚姆勒

user_identity_field: getEmail

相关问题