【问题标题】:401 Bad credentials [lexik/JWTBundle]401 错误凭据 [lexik/JWTBundle]
【发布时间】:2019-06-06 20:13:23
【问题描述】:

我正在构建一个 API。当我创建一个用户时,我会收到一个令牌。但是,当我更改用户信息时无法重新连接,并且收到 401 错误代码和错误凭据消息。请问这个问题怎么解决?

我使用 FOSRestBundle、lexikJWTAuthenticator 和 NelmioCORSBundle

# security.yaml

security:
    encoders:
        App\Entity\User:
            algorithm: argon2i

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email

    firewalls:

        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        signup:
            pattern: ^/api/signup
            stateless: true
            anonymous: true

        signin:
            pattern: ^/api/signin
            stateless: true
            anonymous: true
            json_login: 
                check_path: /api/signin
                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:
            anonymous: true

            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#firewalls-authentication

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true
    role_hierarchy:
        ROLE_ADMIN: ROLE_USER

    # 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/signin, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/signup, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        # - { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: GET }
        # - { path: ^/api, roles: IS_AUTHENTICATED_FULLY, methods: [PUT, DELETE, POST] }
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

# 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

谢谢

【问题讨论】:

    标签: api symfony token http-status-code-401 lexikjwtauthbundle


    【解决方案1】:

    再次尝试重新生成 SSH 密钥

    这里是链接LexikJWT Generate the SSH keys

    并确保Configuration

    中的pass_phrase

    【讨论】:

    • 您使用的是什么网络服务器?
    • 我在带有 bin/console 服务器的 Symfony 服务器上:运行
    • 您是否尝试过使用不在浏览器中的邮递员或其他工具,因为我猜测问题可能来自标头,因为浏览器会自动传递一些标头,因此您可以尝试使用其他工具来检查 API
    • 我使用 postman 和 curl 来测试我的 API,抱歉我忘了指定它
    【解决方案2】:

    您是否尝试过重新生成令牌,或者您使用新令牌得到 401。 如果您更改了用户信息,您应该重新连接并获取新令牌

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-13
      • 2017-04-10
      • 2020-05-20
      • 2013-09-22
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多