Symfony “如何修复”;密钥“用户名必须是字符串”;JWT的错误
我有一个标准连接的网站,我想添加一个新的连接方式,感谢API部分 我遵循了这个文件: 因此,我将JWT捆绑包添加到我的Symfony 3项目中,并更改了我的security.yml:Symfony “如何修复”;密钥“用户名必须是字符串”;JWT的错误,symfony,jwt,Symfony,Jwt,我有一个标准连接的网站,我想添加一个新的连接方式,感谢API部分 我遵循了这个文件: 因此,我将JWT捆绑包添加到我的Symfony 3项目中,并更改了我的security.yml: # app/config/security.yml security: encoders: Symfony\Component\Security\Core\User\User: sha512 GS\UserBundle\Entity\User: sha512 #sha512 role_
# app/config/security.yml
security:
encoders:
Symfony\Component\Security\Core\User\User: sha512
GS\UserBundle\Entity\User: sha512 #sha512
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
providers:
main:
entity:
class: GS\UserBundle\Entity\User
property: login
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: true
provider: main
stateless: true
form_login:
login_path: login
check_path: login_check
default_target_path: preLogin
logout:
path: logout
target: preLogin
remember_me:
secret: '%secret%' # se souvenir de moi
lifetime: 1000
always_remember_me: true
login:
pattern: ^/api/login
stateless: true
anonymous: true
provider: main
json_login:
check_path: /api/login_check
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/api
stateless: true
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: /admin, roles: ROLE_ADMIN }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
我的config.yml配置如下:
lexik_jwt_authentication:
secret_key: '%kernel.project_dir%/config/jwt/private.pem'
public_key: '%kernel.project_dir%/config/jwt/public.pem'
user_identity_field: login
pass_phrase: 'MY PASSWORD'
token_ttl: 3600
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
check_path: /api/login_check
username_parameter: login
password_parameter: mdp
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
当我试图从邮递员那里得到我的代币时,就像这样:
我有一个错误:
The key "_username" must be a string, "NULL" given.
因此,我尝试在security.yml中添加“user_parameter”,以给出我的用户实体的参数名称,如下所示:
lexik_jwt_authentication:
secret_key: '%kernel.project_dir%/config/jwt/private.pem'
public_key: '%kernel.project_dir%/config/jwt/public.pem'
user_identity_field: login
pass_phrase: 'MY PASSWORD'
token_ttl: 3600
login:
pattern: ^/api/login
stateless: true
anonymous: true
json_login:
check_path: /api/login_check
username_parameter: login
password_parameter: mdp
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
现在邮递员什么也不回答我
编辑:
我找到一条命令来检查我的配置文件,我得到了以下答案:
如果我删除username_参数和password_参数,命令检查会告诉我配置正常,但我与postman有相同的错误:
The key "username" must be a string, "NULL" given.
请帮帮我,我找不到任何解决办法 Lexik JWT身份验证配置需要security.yaml中的
提供程序
密钥
login:
pattern: ^/api/login
stateless: true
anonymous: true
provider: YOUR_PRODIVER_NAME #in your case it's main
json_login:
check_path: /api/login_check
username_parameter: login
password_parameter: mdp
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
然后,为了能够验证用户信息,它需要知道哪个字段是身份字段。
因此在lexik\u jwt\u authentication.yaml中:
lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
user_identity_field: YOUR_USER_FIELD # example email
token_ttl: 7200
关于请求参数,必须传递用户名
和密码
(否)
您发送
用户名
(不带\u
)和\u密码
,并且在错误消息\u用户名
中提到了\u
)。那么,可能只是缺少了。
?谢谢,我两个都试过了,但都不起作用。当你说:lexik_jwt_authentication.yaml,它在我的config.yml文件或其他文件中?好的,我试着添加provider并用user_identity_字段配置config.yaml,但仍然不起作用,邮递员回答我空白回答。公共和私有证书可以吗?我想,我在config/jwt文件夹(如文档所示)中生成了我的公钥和私钥,并在config.yml中放入“%kernel.project\u dir%/config/jwt/private.pem”