2015-01-15 15 views
5

Ich benutze LexikJWTBundle für eine RESTful API.Lexik JWT gibt 401 nicht autorisiert zurück

Login funktioniert gut und ich bekomme mein Token. Aber wenn ich eine GET-Anfrage mache, bekomme ich eine 401 ohne Inhalt. Der Autorisierungsheader scheint ok, da ich dies im Profiler erhalten: Anforderungsheader: Zulassung: Inhaber {Token} Anfrage Server Parameter: HTTP_AUTHORIZATION: Bearer {Token}

Die 401 I erhalten ist aus: https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Security/Firewall/JWTListener.php#L80

Ich habe verschiedene Lösungen ausprobiert, aber es funktioniert immer noch nicht.

Hätten Sie eine Idee, wie Sie dies beheben/debuggen können?

Meine config:

# 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 

Und

# 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] } 

Antwort

4

Ich fand nur eine Lösung auf das gleiche Problem

Hier mein 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 

Mein Problem der Benutzername und das Passwort Parameter war. Ich wechsle "username" von "_username" und "Passwort" von "_password"

0

LexikJWTBundle erzeugt Token so Benutzers Anmeldeinformationen gültig sind. Problem tritt auf, wenn Sie versuchen, auf gesicherte Routen zuzugreifen (hinter dem Pfad "^/api").

Sie sollten auf jeden Fall Rollen überprüfen, die dem Benutzer zugewiesen sind. Möglicherweise fehlt ROLE_API und der Benutzer ist nicht vollständig authentifiziert.

+0

Danke für Ihre Antwort. Das Problem war, dass der Provider in der API-Firewall fehlte. Daher muss es in api_login UND api sein. – Xavier13

1

Sie sollten die ROLE_API im role_hierarchy Ihrer security.yml hinzu:

role_hierarchy: 
    # ... 
    ROLE_API: [ROLE_USER] 

Dann können Benutzer mit ROLE_API auf Routen zugreifen, die auf IS_AUTHENTICATED_FULLY beschränkt sind.

Wenn Sie einen Webserver verwenden, versuchen Sie außerdem, Ihre Anwendung mit dem integrierten Server zu verwenden (z. B. app/console server:run).

Apache scheint das Token in den Headern zu ändern.

Verwandte Themen