簡體   English   中英

如何使用 REST API 在 WSO2 中為特定 API 啟用 CORS 配置?

[英]How to enable CORS configuration in WSO2 for a specific API by using REST API?

我正在使用 WSO2 REST API 而不是從它的 UI 創建和發布 API,因為我有數百個 WSO2 API 需要管理。 我使用 swagger 文件(以 json 格式)配置有關我的 API 的所有詳細信息,然后使用 curl 命令發布此 swagger 文件。 我想為我的 WSO2 API 啟用 CORS 配置。

為 WSO2 API 提供的文檔僅提供有關通過 UI 啟用 CORS 配置的信息。 這是鏈接

除了直接從其 UI 之外,我找不到任何關於如何通過任何方式啟用它的信息。 我曾嘗試在 API 的 swagger 文件中添加以下字段,但此更改未反映在已發布的 API 中。

    "CORSConfiguration": {
        "Enabled": "true",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS",
        "Access-Control-Allow-Headers": "authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction",
        "Access-Control-Allow-Credentials": "false"
    }

為特定 API 啟用 CORS 配置的任何幫助將不勝感激。 謝謝 :)

CORS 信息應該像這樣進入 API 創建/更新有效負載。

   "corsConfiguration":    {
      "accessControlAllowOrigins": ["*"],
      "accessControlAllowHeaders":       [
         "authorization",
         "Access-Control-Allow-Origin",
         "Content-Type",
         "SOAPAction"
      ],
      "accessControlAllowMethods":       [
         "GET",
         "PUT",
         "POST",
         "DELETE",
         "PATCH",
         "OPTIONS"
      ],
      "accessControlAllowCredentials": false,
      "corsConfigurationEnabled": false
   }

請參閱 [1] 中的示例有效負載。

[1] https://docs.wso2.com/display/AM260/apidocs/publisher/#!/operations#APIIndividual#apisPost

@Bee,這就是我試圖做的。

{
  "swagger": "2.0",
  "info": {
    "description": "Registration Type Master",
    "version": "1.0",
    "title": "Test_Entity_Master_API",
    "termsOfService": "urn:tos",
    "contact": {"name":"RD"},
    "license": {
      "name": "Apache 2.0",
      "url": "http://www.apache.org/licenses/LICENSE-2.0"
    }
  },
  "host": "http://sampleurl.com/",
  "basePath": "/samplemethod",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/regtype/createregtype": {
      "post": {
        "summary": "Create reg type entry",
        "tags": [
          "Registration Type Master"
        ],
        "deprecated": false,
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "Body",
            "in": "body",
            "required": true,
            "description": "",
            "schema": {
              "type": "object",
              "properties": {
                "key": {
                  "type": "string"
                },
                "value": {
                  "type": "string"
                }
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "schema": {
              "type": "object",
              "properties": {
                "success": {
                  "type": "boolean"
                },
                "error": {
                  "type": "boolean",
                  "default": false
                },
                "message": {
                  "type": "string"
                },
                "data": {
                  "type": "object"
                }
              }
            }
          },
          "500": {
            "description": "",
            "schema": {
              "type": "object",
              "properties": {
                "success": {
                  "type": "boolean",
                  "default": false
                },
                "error": {
                  "type": "boolean"
                },
                "message": {
                  "type": "string"
                },
                "data": {
                  "type": "object"
                }
              }
            }
          }
        }
      }
    }
  },
  "tags": [
    {
      "name": "entity-master-controller",
      "description": "Entity Master Controller"
    }
  ],
     "corsConfiguration":    {
      "accessControlAllowOrigins": ["https://dtdevsso.ril.com"],
      "accessControlAllowHeaders":[
         "authorization",
         "Access-Control-Allow-Origin",
         "Content-Type",
         "SOAPAction"
      ],
      "accessControlAllowMethods":[
         "GET",
         "PUT",
         "POST",
         "DELETE",
         "PATCH",
         "OPTIONS"
      ],
      "accessControlAllowCredentials": "true",
      "corsConfigurationEnabled": "true"
   }
}

在這個 swagger 文件中,盡管添加了 CORS 負載,但在通過 swagger 文件發布 API 后並沒有體現出來。

要設置 CORS 支持,您必須首先在資源中定義一個 OPTIONS 方法,該方法返回所需的標頭。 swagger 中的所有路徑都需要一個 cors 選項塊。 這是塊。

"/users":
   {
    "options": {
        "summary": "CORS support",
        "description": "Enable CORS by returning correct headers\n",
        "consumes": [
            "application/json"
        ],
        "produces": [
            "application/json"
        ],
        "tags": [
            "CORS"
        ],
        "x-amazon-apigateway-integration": {
            "type": "mock",
            "requestTemplates": {
                "application/json": "{\n  \"statusCode\" : 200\n}\n"
            },
            "responses": {
                "default": {
                    "statusCode": "200",
                    "responseParameters": {
                        "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key'",
                        "method.response.header.Access-Control-Allow-Methods": "'*'",
                        "method.response.header.Access-Control-Allow-Origin": "'*'"
                    },
                    "responseTemplates": {
                        "application/json": "{}\n"
                    }
                }
            }
        },
        "responses": {
            "200": {
                "description": "Default response for CORS method",
                "headers": {
                    "Access-Control-Allow-Headers": {
                        "type": "string"
                    },
                    "Access-Control-Allow-Methods": {
                        "type": "string"
                    },
                    "Access-Control-Allow-Origin": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

有關更多詳細信息,您可以訪問此鏈接

我使用以下有效負載在 WSO2 中創建/更新 API。 它運行良好。 很抱歉延遲更新。

{
   "name": "%apiName%",
   "description": "%apiDescription%",
   "context": "/%apiName%",
   "version": "%apiVersion%",
   "provider": "%apiProvider%",
   "apiDefinition": "%swaggger_extended.json% // Input swagger file",
   "wsdlUri": null,
   "status": "CREATED",
   "responseCaching": "Disabled",
   "cacheTimeout": 300,
   "destinationStatsEnabled": false,
   "isDefaultVersion": false,
   "type": "HTTP",
   "transport":    [
      "http",
      "https"
   ],
   "tags": ["%apiTags%"],
   "tiers": ["%apiTiersCollection%"],
   "visibility": "%apiVisibility%",
   "visibleRoles": [],
   "endpointConfig": "%endPointConfig%",
   "gatewayEnvironments": "Production and Sandbox",
   "subscriptionAvailability": null,
   "subscriptionAvailableTenants": [],
   "businessInformation":    {
      "businessOwnerEmail": "%BizOwnerName@ril.com%",
      "technicalOwnerEmail": "%TechOwnerName@ril.com%",
      "technicalOwner": "%TechOwnerName%",
      "businessOwner": "%BizOwnerName%"
   },
   "corsConfiguration":    {
      "accessControlAllowOrigins": ["originURL"],
      "accessControlAllowHeaders":       [
         "authorization",
         "Access-Control-Allow-Origin",
         "Content-Type",
         "SOAPAction"
      ],
      "accessControlAllowMethods":       [
         "GET",
         "PUT",
         "POST",
         "DELETE",
         "PATCH",
         "OPTIONS"
      ],
      "accessControlAllowCredentials": false,
      "corsConfigurationEnabled": true
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM