【问题标题】:How to enable CORS for a single website for my REST API in AppEngine如何在 AppEngine 中为我的 REST API 启用单个网站的 CORS
【发布时间】:2020-01-07 14:43:48
【问题描述】:

我在灵活环境中的 Appengine 中有一个 API。它不支持 CORS。我相信是因为它默认不支持它

After ESP 1.0 is released on January 2, 2017, all Flexible Environment API deployments will feature the new version of ESP and will automatically disallow CORS requests by default. App Engine applications are automatically redeployed every 7 days, so sometime in the 7 days following the release of ESP 1.0, your app will be restarted with the latest version and will automatically be protected from unintended cross origin sharing.

If you are using Flexible Environments and would like to continue to allow CORS requests, you must add the "x-google-endpoints" snippet above to your API configuration (aka OpenAPI specification aka Swagger file). If you are relying on CORS, we recommend that you add the snippet as soon as possible and redeploy your service using the following command to avoid service interruption. Then you will not see changed behavior when the new version of ESP rolls out.

这个页面告诉我设置 allowCors = True 并在我的后端代码中实现支持(它们是指我的 main.go 吗?) https://cloud.google.com/endpoints/docs/openapi/openapi-extensions

此页面告诉我向我的 ESP 添加一些代码,但我不确定它的含义 - 在我的 openapi swagger 文件中? https://cloud.google.com/endpoints/docs/openapi/specify-proxy-startup-options#adding_cors_support_to_esp

本页https://enable-cors.org/server_appengine.html 告诉我添加这段代码,我假设我的 main.go,但这意味着什么?

func doGet(w http.ResponseWriter, r *http.Request) {
  w.Header().Add("Access-Control-Allow-Origin", "*")
  w.Header().Add("Content-Type", "text/csv")
  fmt.Fprintf(w, csvData)
}

我正在努力寻找直接的步骤来为我的 AppEngine API 上的一个网站启用 CORS 支持。有人可以支持吗?

谢谢:)

【问题讨论】:

    标签: rest api google-cloud-platform cors yaml


    【解决方案1】:

    实现它的最直接方法是在 OpenAPI 文档的顶层添加 x-google-extension

    swagger: "2.0"
    host: "my-cool-api.endpoints.my-project-id.cloud.goog"
    x-google-endpoints:
    - name: "my-cool-api.endpoints.my-project-id.cloud.goog"
      allowCors: True
    

    在您找到的第一个link 中有更多信息。

    关于你的问题:

    这个页面https://enable-cors.org/server_appengine.html告诉我添加这个代码,我假设我的main.go,但这是什么意思?

    func doGet(w http.ResponseWriter, r *http.Request) {
      w.Header().Add("Access-Control-Allow-Origin", "*")
      w.Header().Add("Content-Type", "text/csv")
      fmt.Fprintf(w, csvData)
      }
    

    这段代码的作用是在收到 GET 请求时添加 CORS 标头。

    编辑:

    澄清一下,在 Cloud Endpoints 中启用 CORS 只会允许请求到达您的后端,但您必须处理后端代码中的访问限制。

    由于您希望将 CORS 限制为仅一个网页,因此代码将如下所示:

    func doGet(w http.ResponseWriter, r *http.Request) {
          w.Header().Add("Access-Control-Allow-Origin", "https://www.example.com")
          w.Header().Add("Content-Type", "text/csv")
          fmt.Fprintf(w, csvData)
          }
    

    在此示例中,仅允许网站 https://www.example.com

    【讨论】:

    • 谢谢。它允许 CORS,但我不希望启用每个网站,只启用一个网站 - 我如何指定一个网站?
    • 我已经编辑了我的答案,希望您发现它更清楚并帮助您解决问题!
    • 谢谢你说后端代码,你的意思是我把这个函数添加到我的 main.go 和其中的什么地方。我是将它添加到 func main、我的查询处理程序 func 中,还是不在任何 func 中?内容类型文本/csv 位的含义和 fmt.Fprintf(w, csvData)。我没有使用任何与 csv 相关的内容
    • 是的,我的意思是你的 main.go 文件。您必须将其添加到用于处理请求的函数中,即 doGet 函数。 content-type 和 fmt.Fprint 只是网页中的示例,您需要根据您想要实现的功能调整功能。您真正需要的示例中唯一一行是:w.Header().Add("Access-Control-Allow-Origin", "https://www.example.com")
    • 好的,我认为它在 queryHandler 中,所以我会试试这个!我猜 CSV 位是 cors 标头的格式,就像我为 json 响应获得 w.Header().Set("content-type", "application/json") 一样
    猜你喜欢
    • 2019-02-14
    • 2013-03-15
    • 2014-09-01
    • 1970-01-01
    • 2016-02-17
    • 2012-12-26
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多