简体   繁体   English

Falcon CORS 中间件无法正常工作

[英]Falcon CORS middleware does not work properly

I'm using Falcon CORS to allow access to my web service only from several domains.我使用Falcon CORS只允许从多个域访问我的 Web 服务。 But it does not work properly.但它不能正常工作。

Let me explain, if we take a look at my implementation:让我解释一下,如果我们看一下我的实现:

ALLOWED_ORIGINS = ['*']
crossdomain_origin = CORS(allow_origins_list=[ALLOWED_ORIGINS], log_level='DEBUG')

app = falcon.API(middleware=[RequireJSON(), JSONTranslator(), cors.middleware])

When I make any post request to my API service, I get this warning:当我向 API 服务发出任何发布请求时,我收到以下警告:

Aborting response due to origin not allowed由于来源不允许中止响应

But, then I get the correct response from my API.但是,然后我从我的 API 得到了正确的响应。
Here is an official docs about this module: https://github.com/lwcolton/falcon-cors这是关于这个模块的官方文档: https : //github.com/lwcolton/falcon-cors

Your code does not match the falcon-cors documentation's example:您的代码与 falcon-cors 文档的示例不匹配:

import falcon
from falcon_cors import CORS    
cors = CORS(allow_origins_list=['http://test.com:8080'])    
api = falcon.API(middleware=[cors.middleware])
#                            ^^^^^^^^^^^^^^^

Note the cors.middleware variable is being passed into the api call.注意cors.middleware变量被传递到 api 调用中。 In your code you are creating crossdomain_origin but not passing it into the API setup.在您的代码中,您正在创建crossdomain_origin但未将其传递到 API 设置中。

If this does not solve it, please provide a working code example, including the Falcon resource classes, that is easy to test and reproduce, and I'm happy to try to assist.如果这不能解决问题,请提供一个工作代码示例,包括 Falcon 资源类,它易于测试和重现,我很乐意尝试提供帮助。

edit:编辑:

From comments below, it sounds like falcon-cors is working properly, rather the problem may be origin header was being omitted from the request.从下面的评论中,听起来 falcon-cors 工作正常,而问题可能是请求中省略了origin标头。

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

The Origin header indicates the origin of the cross-site access request or preflight request. Origin 头指示跨站点访问请求或预检请求的来源。

I tried as guided by lwcolton on github here在 github 上按照lwcolton 的指导进行尝试

And also set allow_all_headers =True, allow_all_methods =True并设置allow_all_headers =True, allow_all_methods =True

ie same as @Ryan comment即与@Ryan 评论相同

from falcon_cors import CORS

cors = CORS(
    allow_all_origins=True,
    allow_all_headers=True,
    allow_all_methods=True,
)

api = falcon.API(middleware=[cors.middleware])

Side note:边注:

ORIGIN '*' does not work on some browsers.. notably IE. ORIGIN '*' 在某些浏览器上不起作用.. 尤其是 IE。 In the past I've had to dynamically set the ORIGIN header to the 'host' name requested in the HTTP headers in order to support a wildcard domain host for a site I setup.过去,我不得不将 ORIGIN 标头动态设置为 HTTP 标头中请求的“主机”名称,以支持我设置的站点的通配符域主机。

There's is another way to implement this without using falcon-cors还有另一种方法可以在不使用falcon-cors 的情况下实现这一点

You might want to look at this on the official documentation - how-do-i-implement-cors-with-falcon您可能想在官方文档中查看此内容 - how-do-i-implement-cors-with-falcon

class CORSComponent:

    def process_response(self, req, resp, resource, req_succeeded):
        resp.set_header('Access-Control-Allow-Origin', '*')

        if (req_succeeded
            and req.method == 'OPTIONS'
            and req.get_header('Access-Control-Request-Method')
        ):
            # NOTE: This is a CORS preflight request. Patch the
            #   response accordingly.

            allow = resp.get_header('Allow')
            resp.delete_header('Allow')

            allow_headers = req.get_header(
                'Access-Control-Request-Headers',
                default='*'
            )

            resp.set_headers((
                ('Access-Control-Allow-Methods', allow),
                ('Access-Control-Allow-Headers', allow_headers),
                ('Access-Control-Max-Age', '86400'),  # 24 hours
            ))

When using the above approach, OPTIONS requests must also be special-cased in any other middleware or hooks you use for auth, content-negotiation, etc. For example, you will typically skip auth for preflight requests because it is simply unnecessary;使用上述方法时,OPTIONS 请求也必须在用于身份验证、内容协商等的任何其他中间件或挂钩中进行特殊处理。例如,您通常会跳过预检请求的身份验证,因为它根本没有必要; note that such request do not include the Authorization header in any case.请注意,此类请求在任何情况下都不包含 Authorization 标头。

You can now put this in middleware你现在可以把它放在中间件中

api = falcon.API(middleware=[
    CORSComponent()
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM