2017-05-12 3 views
6

Ich habe gerade versucht, meine sf3 API mit einem Login mit JWT zu erstellen, aber ich habe einige Probleme mit der Authentifizierung. Also hier sind meine Konfigurationen und einige Tests, die ich realisiert habe.Symfony 3 + JWT + eckig 2 + Authentifizierung (Optionen Methode)

security.yml:

security: 
    firewalls: 
     login: 
     pattern: ^/api/auth 
     stateless: true 
     anonymous: true 
     form_login: 
      check_path:     /api/auth/login-check 
      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: ~ 
    access_control: 
     - { path: ^/api/auth, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/api,  roles: IS_AUTHENTICATED_FULLY } 

routing.yml:

auth: 
    path:  /auth 
    defaults: { _controller: api.controller.auth:postAction } 
    methods: [OPTIONS, POST] 

api_login_check: 
    path:  /auth/login-check 

config.yml:

nelmio_cors: 
    paths: 
     '^/api/': 
      allow_origin: ['*'] 
      allow_headers: ['*'] 
      allow_methods: ['POST', 'PUT', 'GET', 'DELETE', 'OPTIONS'] 
      max_age: 3600 

Wenn ich das Routing debuggen erhalte ich diese Ausgabe:

$ php bin/console debug:router 
----------------------------------- ------------------ -------- ------ ----------------------------------- 
Name        Method    Scheme Host Path 
----------------------------------- ------------------ -------- ------ ----------------------------------- 
.... 
api_homepage      ANY    ANY  ANY /api/ 
auth        OPTIONS|POST  ANY  ANY /api/auth 
api_login_check      ANY    ANY  ANY /api/auth/login-check 
.... 

So weit so gut, jetzt kommt hier die Probleme.

Ich schaffe mit

$ curl -v -X POST http://api.local/api/auth/login-check -d _username=user -d _password=user 
Note: Unnecessary use of -X or --request, POST is already inferred. 
* Trying 127.0.0.1... 
* TCP_NODELAY set 
* Connected to api.local (127.0.0.1) port 80 (#0) 
> POST /api/auth/login-check HTTP/1.1 
> Host: api.local 
> User-Agent: curl/7.51.0 
> Accept: */* 
> Content-Length: 33 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 33 out of 33 bytes 
< HTTP/1.1 200 OK 
< Server: nginx/1.10.3 
< Content-Type: application/json 
< Transfer-Encoding: chunked 
< Connection: keep-alive 
< X-Powered-By: PHP/7.1.3 
< Set-Cookie: PHPSESSID=c4o6kuelf914gjnq09m38ec0c7; path=/; HttpOnly 
< Cache-Control: no-cache, private 
< Date: Fri, 12 May 2017 16:37:23 GMT 
< Access-Control-Allow-Origin: * 
< Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE, PUT 
< Access-Control-Allow-Credentials: true 
< Access-Control-Allow-Headers: User-Agent,Keep-Alive,Content-Type,Access-Control-Allow-Headers 
< 
* Curl_http_done: called premature == 0 
* Connection #0 to host api.local left intact 
{"token":"b2xlc9.......DW6uAwx4"} 

Groß die Rückkehr der Authentifizierung zu testen, es funktioniert!

Wenn ich die gleiche Anfrage im Browser versuche, erhalte ich einen 404 auf die OPTIONS-Methode, also versuche ich die Anfrage mit einer curl OPTIONS-Anfrage neu zu erstellen (wirklich weiß ich nicht, ob es Sinn macht, aber ich habe es versucht) es trotzdem, wo die Antwort:

$ curl -v -X OPTIONS http://api.local/api/auth/login-check -d _username=user -d _password=user 
* Trying 127.0.0.1... 
* TCP_NODELAY set 
* Connected to api.local (127.0.0.1) port 80 (#0) 
> OPTIONS /api/auth/login-check HTTP/1.1 
> Host: api.local 
> User-Agent: curl/7.51.0 
> Accept: */* 
> Content-Length: 33 
> Content-Type: application/x-www-form-urlencoded 
> 
* upload completely sent off: 33 out of 33 bytes 
< HTTP/1.1 404 Not Found 
< Server: nginx/1.10.3 
< Content-Type: application/json 
< Transfer-Encoding: chunked 
< Connection: keep-alive 
< X-Powered-By: PHP/7.1.3 
< Cache-Control: no-cache, private 
< Date: Fri, 12 May 2017 16:35:49 GMT 
< 
{"error":{"code":404,"message":"Not Found"}} 
* Curl_http_done: called premature == 0 
* Connection #0 to host api.local left intact 

Was ich verstehe, ist, dass der api nicht die Route für die OPTIONS-Methode findet, ich selbst versuchen Methoden hinzuzufügen: [OPTIONS, POST] zum api_login_check Route aber gleicher Ausgang

Getestet mit und ohne nelmioCorsBundle, gleicher Ausgang.

Ich bin ein wenig verloren hier, jeder kann sehen, was mache ich falsch?

dank

Antwort

0

Ihre Auth-Routen sind/oauth auf routing.yml aber Sie verwenden/api/Auth in Ihren Beispielen. Ich denke du hast nur ein paar falsche Konfigurationen.

Aber Ihr echtes Problem ist preflight, Browser tun dies vor Anforderung, das ist die Ursache für Ihre Authentifizierung fehlgeschlagen auf Browser und arbeitet mit CURL.

Ihre App sollte Antwort OPTIONS Anfrage für Routen und sagen Sie dem Browser, welche Methoden oder Header sind erlaubt, um fortzufahren und dies zu erreichen NelmioCorsBundle ist das Beste.

Sie müssen alle Ihre Routen abdecken, Ihre Konfiguration übereinstimmen nur/api Routen und Ihre Anmeldung, versuchen Sie nelmio_cors Konfiguration wie folgt.

nelmio_cors: 
    defaults: 
     allow_credentials: false 
     allow_origin: [] 
     allow_headers: [] 
     allow_methods: [] 
     expose_headers: [] 
     max_age: 0 
     hosts: [] 
     origin_regex: false 
    paths: 
     '^/': 
      allow_origin: ['*'] 
      allow_headers: ['accept', 'authorization', 'content-type'] 
      allow_methods: ['OPTIONS', 'POST', 'PUT', 'PATCH', 'GET', 'DELETE'] 
      max_age: 3600