CORS
This policy controls how your API handles cross-origin requests.
The Cross-Origin Resource Sharing (CORS) mechanism supports secure cross-domain requests and data transfers. It uses specific HTTP headers to define which origins can access selected resources. This policy enables CORS by adding these headers to grant a client or an application permission to access the allowed resources. A cross-origin HTTP request occurs when a client or an application requests a resource from a different domain, protocol, or port than the one used for the current request.
In IBM API Studio, this policy
is identified by the kind CORS.
cors section of the API definition and enforces the rules
defined in that section.- CORS rule processing
-
- The
corssection in the API definition is optional. If you do not specify it or leave it empty, the gateway does not enable CORS handling for the API. - To enable CORS, you must define at least one rule.
- The list of rules can include zero or more allow rules where the
actionis set toallow, and it can include at most onedenyrule, which must appear at the end of the list or be the only rule in the list. - When the gateway receives a CORS request, it evaluates the rules in the order they are listed. The gateway applies the first rule that matches the requested origin. If no rule matches, the gateway denies the request. The gateway applies only one rule for each CORS request.
- The
- Allow rules
-
- An allow rule applies when the requested origin matches an origin in the rule’s
originList. - If the origin matches, the gateway applies the rule. If it does not match, the gateway evaluates the next rule or denies the request if no rule matches.
- An allow rule applies when the requested origin matches an origin in the rule’s
- Deny rules
-
- A deny rule denies the request when the requested origin matches an origin in the rule’s
originList. - If the origin does not match any origin in the
originList, the gateway applies the rule. A deny rule does not continue to another rule.
- A deny rule denies the request when the requested origin matches an origin in the rule’s
Here is a sample code for the CORS policy.
kind: CORS
apiVersion: api.ibm.com/v1
metadata:
name: cors
namespace: wm_policies
version: '1.0'
tags: []
description: ''
spec:
rules:
- action: allow
originList:
- https://www.domain.com
- https://www.test.com
allowCredentials: true
allowMethods:
- GET
allowHeaders:
- header1
- header2
maxAge: 12323
exposeHeaders:
predefined: true
custom: header3, header4
backend: true
For the CORS policy kind, configure these specifications:
| Type | Description |
|---|---|
spec |
This section defines the CORS-related properties you configure for the policy. You can configure the following properties:
|