6

我将 LexikJWTBundle 用于 RESTful API。

登录工作正常,我得到了我的令牌。但是当我发出 GET 请求时,我得到一个没有内容的 401。授权标头似乎没问题,因为我在探查器中得到了这个:请求标头:授权:承载{令牌}请求服务器参数:HTTP_AUTHORIZATION:承载{令牌}

我得到的 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] }
4

3 回答 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”,将“密码”更改“_password”

于 2015-09-22T10:04:41.490 回答
2

您应该ROLE_APIrole_hierarchy您的 security.yml 中添加:

role_hierarchy:
    # ...
    ROLE_API: [ROLE_USER]

然后,排名第 的用户ROLE_API可以访问限制为 的路线IS_AUTHENTICATED_FULLY

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

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

于 2016-02-12T17:49:24.067 回答
0

LexikJWTBundle 生成令牌,因此用户的凭据有效。当您尝试访问安全路由(在“^/api”路径后面)时会出现问题。

您绝对应该检查分配给用户的角色。也许 ROLE_API 丢失并且用户未完全通过身份验证。

于 2015-01-26T14:45:00.413 回答