2015-01-15 126 views
5

我正在将LexikJWTBundle用于RESTful API。Lexik JWT返回401未授权

登录工作得很好,我得到我的令牌。但是当我发出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

我只是发现了同样的问题

这里我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”

0

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

你一定要检查分配给用户的角色。也许ROLE_API丢失,用户没有完全验证。

+0

感谢您的回答。问题在于提供者在api防火墙中缺失。因此它必须在api_login和api中。 – Xavier13

1

您应该添加ROLE_API在security.yml的role_hierarchy

role_hierarchy: 
    # ... 
    ROLE_API: [ROLE_USER] 

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

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

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