无法使用令牌对api平台进行身份验证



我使用symfony api平台设置了一个api,当使用用户凭据(用户名和密码(登录时,api会返回我的令牌,但当我想获得使用该令牌的用户列表时,我会收到以下错误:

{
"code": 401,
"message": "Invalid credentials."
}

下面是我的安全。yaml:

security:
enable_authenticator_manager: true
# https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
password_hashers:
SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface: 'auto'
AppEntityUser:
algorithm: 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: AppEntityUser
property: email
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/api/login
stateless: true
json_login:
check_path: /api/login
username_path: username
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api/
stateless: true
provider: app_user_provider
jwt: ~
main:
lazy: true
provider: app_user_provider
# 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: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/api/docs, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY}
# { path: ^/api/users, roles: IS_AUTHENTICATED_FULLY}
when@test:
security:
password_hashers: 
SymfonyComponentSecurityCoreUserPasswordAuthenticatedUserInterface:
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

有人知道为什么我不能使用有效的令牌登录吗?

***更新***我发送的:

curl -X 'GET' 
'https://127.0.0.1:8001/api/users?page=1' 
-H 'accept: application/ld+json' 
-H 'Authorization: Bearer HERE_IS_MY_TOKEN'

感谢您的帮助

您错误地使用属性email配置了app_user_provider,但在防火墙中使用了username字段。你必须像这样切换你的提供商property

providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: AppEntityUser
property: username # and not email

最新更新