如何使用Flask CORS将网域列入白名单

问题描述 投票:2回答:1

我无法让Flask CORS将某些域列入白名单,而无法执行POST请求。我正在从www.google.com发出卷曲请求,但正在使用域名googl.com来确认该帖子。

当前每个帖子都允许进入,因为我的卷曲是“ Access-Control-Allow-Origin:*”

我只想确保只有设置的域才能发出POST请求

@app.route('/api/userreset', methods=['POST'])
@cross_origin(origin='http://www.googl.com')
    def resetFunction():


   curl --header "Content-Type: application/json"   --request POST "Origin: http://www.google.com" --verbose  --data '{"email":"[email protected]"}'   http://0.0.0.0:8080/api/userreset

我也尝试了下面的无用的方法:

cors = CORS(app, resources={r"/api/*": {"origins": "http://www.googl.com"}})

来自POST的回复如下:

    *   Trying 0.0.0.0...
* TCP_NODELAY set
* Connected to 0.0.0.0 (127.0.0.1) port 8080 (#0)
> POST /api/userreset HTTP/1.1
> Host: 0.0.0.0:8080
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 219
> 
* upload completely sent off: 219 out of 219 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 55
< Access-Control-Allow-Origin: *
< Server: Werkzeug/0.16.0 Python/3.8.0
< Date: Mon, 16 Dec 2019 02:21:49 GMT
python flask flask-cors
1个回答
1
投票

CORS标头告诉浏览器允许访问哪个域。这并不是要完全限制对服务器的访问,而是要告诉浏览器允许从哪个域访问服务器。浏览器仍然必须遵守。如果从CURL执行此操作,它将始终有效,因为curl不会像浏览器那样强制执行CORS。

© www.soinside.com 2019 - 2024. All rights reserved.