简体   繁体   English

如何在AppEngine中为我的REST API启用单个网站的CORS

[英]How to enable CORS for a single website for my REST API in AppEngine

I have an API in Appengine in a flexible environment. 我在灵活的环境中在Appengine中拥有一个API。 It does not support CORS. 它不支持CORS。 I believe it is because it doesn't support it by default 我相信是因为默认情况下它不支持

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.

This page tells me to set allowCors = True and implement support in my backend code (do they mean my main.go?) https://cloud.google.com/endpoints/docs/openapi/openapi-extensions 该页面告诉我设置allowCors = True并在我的后端代码中实现支持(这是否意味着我的main.go?) https://cloud.google.com/endpoints/docs/openapi/openapi-extensions

This page tell me to add some code to my ESP, but I'm not sure where it means - in my openapi swagger file? 该页面告诉我在ESP中添加一些代码,但是我不确定它的含义-在我的openapi swagger文件中? https://cloud.google.com/endpoints/docs/openapi/specify-proxy-startup-options#adding_cors_support_to_esp https://cloud.google.com/endpoints/docs/openapi/specify-proxy-startup-options#adding_cors_support_to_esp

This page https://enable-cors.org/server_appengine.html tells me to add this code, I assume to my main.go, but what does it mean? 此页面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)
}

I am struggling to find straight forward steps to enable CORS support for one website on my AppEngine API. 我正在努力寻找直接的步骤,以在我的AppEngine API上为一个网站启用CORS支持。 Can someone support please? 有人可以支持吗?

Thanks :) 谢谢 :)

The most straightforward way to achieve it is to add the x-google-extension at the top level of your OpenAPI document: 实现此目的最直接的方法是在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

There's more information regarding that in the first link you found. 在您找到的第一个链接中有关于此的更多信息。

Regarding your question: 关于您的问题:

This page https://enable-cors.org/server_appengine.html tells me to add this code, I assume to my main.go, but what does it mean? 此页面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)
  }

That piece of code what will do is add the CORS headers whenever a GET request is received. 该段代码将在接收到GET请求时添加CORS标头。

EDIT: 编辑:

Just to clarify, enabling CORS in Cloud Endpoints will only allow the request to hit your backend, but you have to handle the access restriction in your backend code. 需要澄清的是,在Cloud Endpoints中启用CORS仅会允许请求到达您的后端,但是您必须处理后端代码中的访问限制。

As you want to restrict the CORS to only one webpage, the code will then look like: 由于您希望将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)
      }

With this example, only the website https://www.example.com will be allowed. 在此示例中,仅允许使用网站https://www.example.com

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

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