首页 > 解决方案 > Symfony 4 + API Plantform +LexikJWTAuthenticationBundle 坏凭证

问题描述

下午好。请我在 symfony 4 api 项目中使用 LexikJWTAuthenticationBundle。我正在将 UserProvider 用于 Doctrine。

在配置 Doctrine User Provider 之后,我已经安装并配置了 LexikJWTAuthenticationBundle。但是,当我尝试在 URL http://localhost:8000/api/login_check上使用 Postman 验证这个 JSON {"username":"ak", "password":"ak"} 我有这个错误:{"代码”:401,“消息”:“错误凭据”}。请参阅下面我的 Security.yaml 配置文件。我已经阅读论坛试图解决这个问题,但我还没有找到解决方案。你能帮我么?

security:
encoders:
    App\Entity\Utilisateur:
        algorithm: bcrypt

providers:
    #in_memory: { memory: ~ }
    our_db_provider:
        entity:
            class: App\Entity\Utilisateur

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    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

    main:
        pattern:    ^/
        user_checker: App\Security\UtilisateurChecker
        anonymous: true
        provider: our_db_provider         

access_control:
    - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

标签: symfonylexikjwtauthbundle

解决方案


您在安装过程中是否遗漏了一项配置?我没有看到lexik:

lexik_jwt_authentication:
    secret_key:       '%kernel.project_dir%/config/jwt/private.pem' # required for token creation
    public_key:       '%kernel.project_dir%/config/jwt/public.pem'  # required for token verification
    pass_phrase:      'your_secret_passphrase' # required for token creation, usage of an environment variable is recommended
    token_ttl:        3600

推荐阅读