gpt4 book ai didi

php - Symfony 安全组件 - 无法在 token 负载中找到 key \"username\"

转载 作者:行者123 更新时间:2023-12-04 16:00:32 26 4
gpt4 key购买 nike

使用下面列出的这个配置,我遇到了一些我无法单独解决的奇怪问题,因为我在 Symfony 中太新了。

security:
encoders:
App\Api\User\Entity\User:
algorithm: bcrypt
cost: 12
# https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
providers:
users:
id: 'App\Api\Auth\Provider\AuthProvider'
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
# Api will be stateless, a token will be generated
api_login:
pattern: /api/login
stateless: true
context: api
anonymous: true
provider: users
form_login:
check_path: /api/login
username_parameter: email
password_parameter: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
# For web calls no need to be stateless
web_login:
pattern: /login
stateless: false
context: web
anonymous: true
provider: users
guard:
entry_point: 'App\User\Auth\Guard\LoginAuthenticator'
authenticators:
- 'App\Api\Auth\Guard\LoginAuthenticator'
form_login:
login_path: /login
check_path: /login
api:
provider: users
context: api
pattern: ^/api
stateless: true
anonymous: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
web:
provider: users
context: web
pattern: ^(/user|/template)
stateless: false
anonymous: true
guard:
authenticators:
- 'App\Api\Auth\Guard\LoginAuthenticator'
main:
pattern: ^/
anonymous: ~
logout:
path: /logout
target: /login



# 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, roles: ROLE_USER }
- { path: ^/user/*, roles: ROLE_USER }
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

我有 :
  • 带有状态模式的安全区域的后台
  • 具有无状态模式的安全区域的 API(JWT token )

  • 现在这是什么工作:
  • 我可以连接我的 HTML 表单并查看我的角色和我的数据(电子邮件)
  • 当我连接 (HTML) 时,我会立即重定向到/user/profile
    端点并查看我的信息
  • 我可以调用我的 API 登录端点并获取 JWT token

  • 这里有什么不工作
  • 从 API 调用中,我有一个没有用户名(空)的 token
  • 如果您尝试从 api 访问安全区域,我会列出一个错误
    下面

  • 我解码的 token :

    {"alg":"RS256"}{"roles":["ROLE_USER"],"username":null,"iat":1527787676,"exp":1527791276}


    错误:
    {
    "code": 401,
    "message": "Unable to find key \"username\" in the token payload."
    }

    任何的想法 ?

    最佳答案

    我的身份字段是电子邮件,而不是用户名。

    在 jwt 配置文件中,我只需要输入:

    lexik_jwt_authentication:
    private_key_path: '%kernel.project_dir%/%env(JWT_PRIVATE_KEY_PATH)%'
    public_key_path: '%kernel.project_dir%/%env(JWT_PUBLIC_KEY_PATH)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
    user_identity_field: email #this line

    关于php - Symfony 安全组件 - 无法在 token 负载中找到 key \"username\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50629890/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com