Let's take a look at some concepts and terminology that you need to know in order to follow the use cases.
...
Cross-origin resource sharing ( CORS ) is a mechanism that allows restricted resources (e.g., fonts, JavaScript) of a Web page to be requested from another domain outside the domain from which the resource originated.
The Swagger API Console that is integrated in the API Manager runs as a JavaScript client in the API Store and makes calls from the Store to the API Gateway. Therefore, if you have the API Store and Gateway running on different ports, enable CORS between them.
...
The elements are described below:
Header | Description |
---|
Sample values |
---|
Access-Control-Allow- |
https://localhost:9443,http://localhost:9763
<Access-Control-Allow-Origin header>
. Default values are API Store addresses that are required for swagger to function properly.Header values you need to pass when invoking the API. For example, authorization, Access-Control-Allow-Origin, Content-Type, SOAPAction
True/False
...
Origin | Determines whether a resource can be shared with the resource of a given origin. The API Gateway validates the origin request header value against the list of origins defined under the Access Control Allow Origins configuration(this can be All Allow Origins or a specific value like localhost ). If the host is in the allowed origin list, it will be set as the Access-Control-Allow-Origin response header in the response. | All Allow Origins(*), localhost |
Access-Control-Allow-Headers | Determines, as part of the response to a preflight request (a request that checks to see if the CORS protocol is understood), which header field names can be used during the actual request. The gateway will set the header values defined under Access Control Allow Headers configurations. | authorization, Access-Control-Allow-Origin, Content-type, SOAPAction |
Access-Control-Allow-Methods | This header specifies the method(s) allowed when accessing the resource in response to a preflight request. Required methods can be defined under the Access Control Allow Method configuration. | GET, PUT, POST, DELETE, PATCH, OPTIONS |
Access-Control-Allow-Credentials | Determines whether or not the response to the request can be exposed to the page. It can be exposed when the header value is true. The header value can be set to true/false by enabling/disabling the Access Control Allow Credentials configuration. | true, false |
If you try to invoke an API with inline endpoints, you add the CORS Handler in the <handlers>
section of the API's configuration as follows. Find the API's configuration in the <APIM_HOME>/repository/deployment/server/synapse-configs/default/api
folder.Change your code according to the sample given here.
Code Block | ||
---|---|---|
| ||
<handlers> <handler class="org.wso2.carbon.apimgt.gateway.handlers.security.CORSRequestHandler"/> </handlers> |
...
OAuth scopes
Scopes enable fine-grained access control to API resources based on user roles. You define scopes to an API's resources. When a user invokes the API, his/her OAuth 2 bearer token cannot grant access to any API resource beyond its associated scopes.
...