Cross-origin resource sharing (CORS) controls which external origins can access your API resources through a browser. You can configure route-level CORS policies to restrict allowed origins, HTTP methods, and headers.
Procedure
-
You can configure a CORS policy from either inside or outside an instance:
APIs outside an instance
-
Log on to the Cloud-native API Gateway console. In the left-side navigation pane, choose API, and then select a region.
-
Click the target API. From the instance drop-down list, select the target instance or All instances.
-
Select the target route from the Routes list.
APIs inside an instance
-
Log on to the Cloud-native API Gateway console. In the left-side navigation pane, choose Instance, and then select a region.
-
On the Instance page, click the target instance. In the left-side navigation pane, choose API and click the target API.
-
Select the target route from the Routes list.
-
-
Click the Configure Policy tab, and then click Enable Policy/Plug-in in the Inbound Processing section.
-
Click the CORS card. In the Add Policy: CORS panel, configure the parameters and then click Add.
NoteCORS policies do not apply to mock services. Use a real backend service for testing.
Parameter
Description
Enable
Turn on the Enable switch.
-
Enabled: Specified third-party origins can access your server resources through a browser.
-
Disabled: No third-party origins can access your server resources through a browser.
Allowed Origins
The origins allowed to access your server resources. Supported formats:
-
All origins:
*. -
Wildcard subdomain match:
*.example.com. -
Multiple specific origins: enter each origin on a separate line, starting with
http://orhttps://.
NoteMaps to the
Access-Control-Allow-Originheader. When a request'sOriginmatches an allowed origin, the responseAccess-Control-Allow-Originheader returns thatOriginvalue.Allowed Methods
The HTTP methods allowed for cross-origin requests. Common methods: GET, POST, PUT, DELETE, HEAD, OPTIONS, and PATCH.
NoteMaps to the Access-Control-Allow-Methods header.
Allowed Request Headers
Non-standard request headers allowed in cross-origin requests.
-
All headers:
*. -
Multiple specific headers: enter each name on a separate line.
NoteMaps to the Access-Control-Allow-Headers header.
Allowed Response Headers
Response headers exposed to client-side scripts.
-
All headers:
*. -
Multiple specific headers: enter each name on a separate line.
NoteMaps to the Access-Control-Expose-Headers header.
Allow to Carry Credentials
Whether cross-origin requests can include credentials such as cookies and HTTP authentication.
NoteMaps to the Access-Control-Allow-Credentials header.
Precheck Expiration Time
The duration in seconds that a browser can cache preflight (OPTIONS) response results for non-simple requests.
NoteMaps to the Access-Control-Max-Age header.
-
Verification
-
Send a test request:
curl -I -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET" -H 'Host: www.test.com' -X OPTIONS http://121.196.XX.XX/demo/item/list -
Expected output:
HTTP/1.1 200 OK allow: GET,HEAD,OPTIONS x-content-type-options: nosniff x-xss-protection: 1; mode=block cache-control: no-cache, no-store, max-age=0, must-revalidate pragma: no-cache expires: 0 x-frame-options: DENY content-length: 0 date: Tue, 30 Nov 2021 03:20:31 GMT x-envoy-upstream-service-time: 6 access-control-allow-origin: http://example.com access-control-allow-credentials: true access-control-allow-methods: GET,POST,PUT,DELETE,HEAD,OPTIONS access-control-expose-headers: * server: istio-envoy
References
Learn more about CORS on MDN.