Symfony 3 + JWT + angular 2 + 身份验证(选项方法)
Posted
技术标签:
【中文标题】Symfony 3 + JWT + angular 2 + 身份验证(选项方法)【英文标题】:Symfony 3 + JWT + angular 2 + Authentication (Options Method) 【发布时间】:2017-10-12 01:43:36 【问题描述】:我只是尝试使用 JWT 登录创建我的 sf3 api,但我在身份验证方面遇到了一些问题。所以这是我的配置和我实现的一些测试。
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
当我调试路由时,我会收到以下输出:
$ 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
....
到目前为止一切顺利,现在问题来了。
我设法测试了身份验证的返回
$ 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"
很好,它正在工作!
当我在浏览器中尝试相同的请求时,我在 OPTIONS 方法上收到 404,所以我尝试使用 curl OPTIONS 请求重新创建请求(我真的不知道这是否有意义,但我试过了无论如何,在哪里响应:
$ 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
我的理解是api没有找到OPTIONS方法的路由,我什至尝试将methods: [OPTIONS, POST]添加到api_login_check路由但输出相同。
使用和不使用 nelmioCorsBundle 进行测试,输出相同。
我有点迷路了,任何人都可以看到我做错了什么?
谢谢
【问题讨论】:
【参考方案1】:您的身份验证路由是 routing.yml 上的 /oauth,但您在示例中使用的是 /api/auth。我认为您的配置有点错误。
但您真正的问题是preflight,浏览器在请求之前执行此操作,这是您的身份验证在浏览器上失败并使用 CURL 的原因。
您的应用应响应路由的 OPTIONS 请求,并告诉浏览器允许继续使用哪些方法或标头,以实现这一目标NelmioCorsBundle 是最好的。
你需要覆盖你所有的路由,你的配置只匹配 /api 路由和你的登录,试试这样的 nelmio_cors 配置。
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
【讨论】:
以上是关于Symfony 3 + JWT + angular 2 + 身份验证(选项方法)的主要内容,如果未能解决你的问题,请参考以下文章
使用 Symfony 5.3 检查 JWT (Firebase) 令牌