0

我是 apiman 的新手,我正在尝试将一些 API 调用与 Jquery 一起使用。但不幸的是,我收到了这个错误:

XMLHttpRequest 无法加载 https://apigtw.url/apiman-gateway/ //1.0/?apikey=9999999-8888-6666-33333-968a712ce68b。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“ http://myapp.local ”。响应的 HTTP 状态代码为 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中解决,但仍然发生在我身上。我正在使用 APIMan 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);
  }
});
4

1 回答 1

2

您是否确保 CORS 策略在策略链中位于 Keycloak 策略之前?这将确保首先服务 CORS。

即CORS Policy -> Keycloak Policy -> Authorization Policy

注意:如果您使用 CORS 策略,那么您可能应该将您的 API 密钥作为查询参数传递,因为浏览器不会在预检请求期间传递自定义标头(它代表您执行)。

请参阅:https ://stackoverflow.com/a/13997235/2766538

于 2016-12-28T14:36:00.300 回答