Lexik JWT返回401未经授权。

6
我正在使用LexikJWTBundle来构建RESTful API。登录功能运作正常,我能够获得token,但在进行GET请求时,我会收到一个没有内容的401错误。已验证Authorization标头无误,因为在Profiler中我能看到以下信息: 请求标头:authorization: Bearer {token} 请求服务器参数:HTTP_AUTHORIZATION: Bearer {token}
我的401错误来自于此处:https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Security/Firewall/JWTListener.php#L80 我尝试了不同的解决方案,但仍未解决问题。你有任何想法可以帮助我解决或调试这个问题吗?
我的配置如下:
# config.yml
...

lexik_jwt_authentication:
    private_key_path: %kernel.root_dir%/var/jwt/private.pem   # ssh private key path
    public_key_path:  %kernel.root_dir%/var/jwt/public.pem    # ssh public key path
    pass_phrase:      'TEST'                                      # ssh key pass phrase
    token_ttl:        86400                                   # token ttl - defaults to 86400

并且

# security.yml

security:
role_hierarchy:
    ROLE_SUPER_ADMIN:       [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
# http://sonata-project.org/bundles/admin/2-3/doc/reference/security.html
# set access_strategy to unanimous, else you may have unexpected behaviors
access_decision_manager:
    strategy: unanimous

encoders:
    FOS\UserBundle\Model\UserInterface: sha512

providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email

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

    api_login:
        pattern: ^/api/login # Default: .*
        provider: fos_userbundle

        # form login
        form_login:
            login_path:     fos_user_security_login
            # csrf_provider: form.csrf_provider # Default: my.csrf_provider.id
            # LexikJWT # 09/01/15 - Note: provient de la configuration officielle.
            check_path:     api_login_check
            success_handler:          lexik_jwt_authentication.handler.authentication_success
            failure_handler:          lexik_jwt_authentication.handler.authentication_failure
            require_previous_session: false 
        anonymous: true # Default: ~

    api:
        pattern:   ^/api
        stateless: true
        lexik_jwt:
            authorization_header: # check token in Authorization Header
                enabled: true
                prefix:  Bearer
        anonymous: true

access_control:
    # Secured part of the site
    # This config requires being logged for the whole site and having the admin role for the admin part.
    # Change these rules to adapt them to your needs
    - { path: "^/api/contacts$", roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [POST] }
    - { path: "^/api/users/dt$", roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [GET] }
    - { path: "^/api/users$", roles: IS_AUTHENTICATED_ANONYMOUSLY, methods: [POST] }
    - { path: "^/api",       roles: [IS_AUTHENTICATED_FULLY, ROLE_API] }
3个回答

4
我在相同的问题上找到了解决方案。
这是我的security.yml文件:
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt|error)|css|images|js)/
        security: false

    login:
        pattern:  ^/api/login
        stateless: true
        anonymous: true
        provider:   user_db
        form_login:
            check_path:               /api/login_check
            username_parameter:       _username
            password_parameter:       _password
            success_handler:          lexik_jwt_authentication.handler.authentication_success
            failure_handler:          lexik_jwt_authentication.handler.authentication_failure
            require_previous_session: false

    api:
        pattern:   ^/api
        stateless: true
        lexik_jwt:
            authorization_header: # check token in Authorization Header
                enabled: true
                prefix:  Bearer
            throw_exceptions:        false     # When an authentication failure occurs, return a 401 response immediately
            create_entry_point:      true      # When no authentication details are provided, create a default entry point that returns a 401 response
            authentication_provider: lexik_jwt_authentication.security.authentication.provider

我的问题在于用户名和密码参数。我把"username"改成了"_username",把"password"改成了"_password"


2

您需要在security.yml的role_hierarchy中添加ROLE_API

role_hierarchy:
    # ...
    ROLE_API: [ROLE_USER]

然后,拥有ROLE_API身份的用户可以访问被IS_AUTHENTICATED_FULLY限制的路由。

此外,如果您正在使用Web服务器,请尝试使用内置服务器运行应用程序(即app/console server:run)。

Apache似乎会修改标头中的令牌。


0

LexikJWTBundle 会生成令牌,以此来验证用户的凭据。

当您尝试访问受保护的路由(在"^/api"路径下)时,可能会遇到问题。

您应该检查分配给用户的角色。也许缺少 ROLE_API 角色,用户没有完全通过身份验证。


感谢您的回答。问题在于API防火墙中缺少提供程序。因此,它必须在api_login和api中。 - Xavier13

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接