如何在APIMan中配置CORS

问题描述:

我是apiman的新手,我试图在JQuery中使用一些API调用。但不幸的是我得到这个错误:如何在APIMan中配置CORS

XMLHttpRequest cannot load https://apigtw.url/apiman-gateway/ //1.0/?apikey=9999999-8888-6666-33333-968a712ce68b. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://myapp.local ' is therefore not allowed access. The response had HTTP status code 500.

所以,我使用的一些政策,如:

Keycloak授权策略:

Require OAuth: true 
Require Transport Security: true 
Blacklist Unsafe Tokens: false 
Realm: https://sso.local/auth/realms/test-realm 
Keycloak Realm Certificate: <keycloak key certificate> 
Forward Realm Roles? true 

授权策略配置:

Path: .* 
Verb: * 
Required Role: my-role 

我错过了apiman配置上的东西吗?

我相信我应该加CORs,但我不知道是否有必要。我试图使用这个,但错误仍然发生。

请需要帮助

有谁知道如何配置APIMan CORS?

编辑:

看来这是在issue 516解决,但仍与我发生。我使用API​​Man

这里的版本1.2.3是响应头:

Connection:close 
Content-Type:application/json 
Date:Wed, 28 Dec 2016 13:54:08 GMT 
Server:Apache/2.4.18 (Ubuntu) 
Transfer-Encoding:chunked 
X-Gateway-Error:API not public. 
X-Powered-By:Undertow/1 

这里是请求头:

Accept:*/* 
Accept-Encoding:gzip, deflate, sdch, br 
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 
Access-Control-Request-Headers:authorization, x-api-key 
Access-Control-Request-Method:GET 
Connection:keep-alive 
Host: apiman.url 
Origin:http://192.168.56.22:8080 
Referer:http://192.168.56.22:8080/app 
User-Agent:Mozilla/5.0 ... 
Query String Parameters 
view source 
view URL encoded 

这里是我的ajax请求:

$.ajax({ 
    url: 'https://apiman.url/apiman-gateway/<org>/<api>/1.0/<method>?apikey=xxxxxx-xxxxx-xxxxx-xxxxx', 
    headers: { 
     'Content-Type':'application/json', 
     'Accept' : 'application/json', 
     'Authorization' : 'Bearer ' + keycloak.token 
    }, 
    method: 'GET', 
    dataType: 'json', 
    success: function(data){ 
    console.log('header1', data); 
    } 
}); 
+0

的可能的复制[“否‘访问控制允许来源’标题存在于所请求的资源”](http://*.com/questions/20035101/no-access -control-allow-origin-header-is-present-on-the-requested-resource) – Dekel

+0

谢谢!该链接有一个类似的内容,可以帮助我。但我相信我可以通过此链接添加更多信息['CORS?当然!'](http://www.apiman.io/blog/security/plugin/policy/cors/1.2.x/2016/01/22/cors-redux.html)。 –

+0

也许这个问题太笼统了。我会改变这个 –

您确定CORS政策是之前政策链中的Keycloak政策吗?这将确保CORS首先得到服务。

即CORS策略 - > Keycloak政策 - >授权策略

注:如果您使用的CORS策略,那么你或许应该通过你的API密钥作为查询参数,因为浏览器不会在飞行前通过自定义页眉请求(代表您执行)。

参见:https://*.com/a/13997235/2766538

+0

谢谢@Marc! 这只是我得到的政策。顺便说一句,我不这样做,如果这可能是有用的,但我把这个政策在我的客户和API之间的计划。 –

+0

现在,我只是删除keycloak和授权策略。现在我试图了解为什么请求没有考虑我的请求标头 –

+0

从您提供的编辑 - 错误表明您没有使用正确的URL访问API。您应该使用客户端选项卡提供的网址 - 您似乎试图直接访问它。 –