使用LexikJWTAuthenticationBundle时无效的JSON消息

时间:2019-03-02 14:22:40

标签: php symfony curl jwt lexikjwtauthbundle

我有一个Symfony 4项目,需要在其中使用LexikJWTAuthenticationBundle执行安全认证。

我通过遵循其文档来实现捆绑软件​​,并且在执行以下命令时收到此错误:curl -X POST -H "Content-Type: application/json" http://localhost:8000/api/login_check -d '{"username":"johndoe","password":"test"}'

错误消息:

  

无效的JSON

此错误的类型为Bad Request(400),它在文件中的json_decode时发生:

  

Symfony \ Component \ Security \ Http \ Firewall \ UsernamePasswordJsonAuthenticationListener

security.yaml:

providers:
    user:
        entity:
            class: App\Entity\User
            property: email

encoders:
    App\Entity\User: bcrypt

role_hierarchy:
    ROLE_VIP:  ROLE_USER
    ROLE_ADMIN:       [ROLE_USER, ROLE_VIP ]
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_VIP, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

firewalls:
    login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        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


    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        pattern: ^/
        anonymous: true
        form_login:
            username_parameter: "login_form[email]"
            password_parameter: "login_form[password]"
            login_path: site_accueil
            check_path: site_accueil
            default_target_path: site_espace_vip_index
            remember_me: false
            success_handler: redirect.after.login
        logout:
            path: /deconnexion
            target: /
        guard:
            authenticators:
                - App\Security\LoginFormAuthenticator
access_control:
- { path: ^/connexion-vip, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/interface, roles: [ROLE_ADMIN] }
- { path: ^/profile, roles: [ROLE_VIP, ROLE_ADMIN] }
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

routes.yaml:

api_login_check:
    path: /api/login_check

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: 3600

已生成SSH密钥。

请问是什么问题?

1 个答案:

答案 0 :(得分:2)

问题已解决:curl -X POST -H "Content-Type: application/json" http://localhost:8000/api/login_check -d "{\"username\":\"johndoe\",\"password\":\"test\"}"