JS 无法从 orion-ld 代理接收 JSON 数据,发生了 CORS 相关问题

发布于 2025-01-10 07:30:15 字数 2024 浏览 1 评论 0原文

我使用 orion-ld 上下文代理

{
"orionld version": "post-v1.0.0",
"orion version": "1.15.0-next",
"uptime": "0 d, 0 h, 25 m, 17 s",
"git_hash": "nogitversion",
"compile_time": "Wed Jan 26 15:58:47 UTC 2022",
"compiled_by": "root",
"compiled_in": "",
"release_date": "Wed Jan 26 15:58:47 UTC 2022",
"doc": "https://fiware-orion.readthedocs.org/en/master/"
}

如果我尝试通过 JS 请求 HTML 文件中的 JSON 数据,就像

var myRequest = new XMLHttpRequest();
myRequest.open("GET", "http://192.168.0.18:1028/ngsi-ld/v1/entities/urn:ngsi-ld:OffStreetParking:XXX_102700") ;
myRequest.setRequestHeader("Accept", "application/ld+json")       
myRequest.onload = function () {
    console.log(myRequest.responseText);
};
myRequest.send();

我收到错误:“访问 XMLHttpRequest at 'http://192.168.0.18:1028/ngsi-ld/v1/entities/来自来源“http://192.168.0.18”的 urn:ngsi-ld:OffStreetParking:XXX_102700' 已被 CORS 阻止策略:请求的资源上不存在“Access-Control-Allow-Origin”标头。”

如果我登录到 docker 容器控制台并检查变量 ORIONLD_CORS_ALLOWED_ORIGIN,它包含必要的值“__ALL”。

这里可能出了什么问题?

更新于 2022 年 3 月 1 日: 现在我基于 NGSI 的 JS 函数创建一个 简单网页 .js,正如 Jason Fox 昨天建议的那样。在那里,您可以测试对 orion-ld 的“v2”相关调用和“ld”相关调用。 在浏览器控制台中,您可以看到记录的结果。 您还可以更改网站上的 orion-ld URL,以便测试您的环境。 然而,orion-ld 服务器的 CORS 设置并未按预期工作。 “v2”调用工作正常,“ld”调用失败。 我使用以下 docker-compose.yml 创建 orion-ld 测试环境:

version: "3.5"
services:
  orion-ld:
    image: fiware/orion-ld
    hostname: orion-ld
    ports:
      - "1029:1026"
    depends_on:
      - mongo-db  
    environment:
      ORIONLD_CORS_ALLOWED_ORIGIN: __ALL
      corsOrigin: __ALL
      ORIONLD_CORS_MAX_AGE: 86400
    command: -dbhost mongo-db-vm -logLevel DEBUG

  mongo-db:
    image: mongo:3.6 
    hostname: mongo-db-vm
    volumes:
      - /SOMEWHERE/data:/data

请在 简单网页并提供反馈。

I use orion-ld context broker

{
"orionld version": "post-v1.0.0",
"orion version": "1.15.0-next",
"uptime": "0 d, 0 h, 25 m, 17 s",
"git_hash": "nogitversion",
"compile_time": "Wed Jan 26 15:58:47 UTC 2022",
"compiled_by": "root",
"compiled_in": "",
"release_date": "Wed Jan 26 15:58:47 UTC 2022",
"doc": "https://fiware-orion.readthedocs.org/en/master/"
}

If I try to request JSON data in an HTML file via JS like

var myRequest = new XMLHttpRequest();
myRequest.open("GET", "http://192.168.0.18:1028/ngsi-ld/v1/entities/urn:ngsi-ld:OffStreetParking:XXX_102700") ;
myRequest.setRequestHeader("Accept", "application/ld+json")       
myRequest.onload = function () {
    console.log(myRequest.responseText);
};
myRequest.send();

I got a error: "Access to XMLHttpRequest at 'http://192.168.0.18:1028/ngsi-ld/v1/entities/urn:ngsi-ld:OffStreetParking:XXX_102700' from origin 'http://192.168.0.18' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

If I login to the docker container console and check the variable ORIONLD_CORS_ALLOWED_ORIGIN, it contains the necessary value "__ALL".

What could be wrong here?

Update at 03/01/2022:
Now I create a simple webpage based on JS functions from NGSI.js, as Jason Fox yesterday suggested. There you are able to test the "v2" related calls to the orion-ld and the "ld" related calls.
In the browser console, you can see the logged results.
You can also change the orion-ld URL on the website so that you can test your environment.
Nevertheless, the CORS settings for orion-ld server did not work as expected. 'v2' calls work well, 'ld' calls failed.
I create my orion-ld test environment with the following docker-compose.yml:

version: "3.5"
services:
  orion-ld:
    image: fiware/orion-ld
    hostname: orion-ld
    ports:
      - "1029:1026"
    depends_on:
      - mongo-db  
    environment:
      ORIONLD_CORS_ALLOWED_ORIGIN: __ALL
      corsOrigin: __ALL
      ORIONLD_CORS_MAX_AGE: 86400
    command: -dbhost mongo-db-vm -logLevel DEBUG

  mongo-db:
    image: mongo:3.6 
    hostname: mongo-db-vm
    volumes:
      - /SOMEWHERE/data:/data

Please test my and your environment on simple webpage and give feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2025-01-17 07:30:15

这里是 Orion-LD 架构师和开发人员。 Orion-LD 关于 CORS 的所有内容都是从 Orion 继承的。这是第一次有人为 Orion-LD 请求 CORS,所以,我会尽力尽快修复它。

Orion-LD architect and developer here. All Orion-LD has about CORS is what was inherited from Orion. This is the first time anybody asks for CORS for Orion-LD, so, I'll try to fix it asap.

玩物 2025-01-17 07:30:15

我想这都是关于本地开发设置的。因此,您可能会在“localhost”或“127.0.0.1”处调用前端,当它在“192.168.0.18”处调用 Orion 时,会导致 CORS 问题。基本上,您有 2 个选项可以使其正常工作:

  1. 开发时在浏览器中禁用 CORS。具体解决方案取决于您的浏览器。
  2. 将 Orion-LD 放在本地主机上,从而使用与前端相同的来源来调用它。您发布的 docker-compose 片段已将 orion 放在“localhost:1029”/“127.0.0.1:1029”上(请参阅端口部分)。因此,您应该将前端中的 orion 端点配置为“localhost:1029”/“127.0.0.1:1029”(取决于调用 orion 时使用的地址)而不是当前容器 IP(例如“192.168.0.18: 1028")

我认为不值得直接在 Orion-LD 中实现 CORS 支持,因为这不是真正的部署场景。在现实场景中,您可能会遇到以下情况之一:

  1. 一个非常小的场景,甚至不使用任何类型的安全框架,并且仅使用一个代理实例。在这种环境中,前端在不同的源上运行是非常不寻常的。
  2. orion 和前端之间的安全框架。在这种情况下,来自前端的请求永远不会直接到达代理,而是到达安全层。因此,安全组件必须提供 CORS 标头。
  3. 具有任何类型附加基础设施的场景(最有可能是负载均衡器或 k8s 环境中的 lb+入口控制器)。与场景 2 类似,这将导致没有请求直接到达代理,并且前端和代理之间的组件需要处理 CORS。

I guess this is all about a local development setup. Thus you probably call your frontend at "localhost" or "127.0.0.1", leading to the CORS-issue when it calls orion at "192.168.0.18". You basically have 2 options to still make it work:

  1. disable CORS in your browser when developing. Concrete solution depends on your browser.
  2. Put Orion-LD on localhost, thus calling it with the same origin as your frontend. The docker-compose snippet that you posted already puts orion on "localhost:1029"/"127.0.0.1:1029"(see ports-section). Thus, you should configure the orion endpoint in your frontend to "localhost:1029"/"127.0.0.1:1029"(depending on the address you use when calling orion) instead of the current container-ip(e.g. "192.168.0.18:1028")

I do not think it worth implementing the CORS-support directly in Orion-LD, since this is not a real deployment scenario. In real-world scenarios, you would have one of the following:

  1. A very small scenario, that does not even use any kind of security framework and only one broker instance. In such environments, its very unusual to have the frontend running on a different origin.
  2. A security framework between orion and the frontend. In this case, the requests from the frontend will never directly reach the broker, but the security-layer instead. Thus, the security component has to provide the CORS-header.
  3. A scenario with any kind of additional infrastructure(most probably a load-balancer or f.e. an lb+an ingress controller in k8s-environemnts). Similar to scenario 2, this would lead to no request directly reaching the broker and the component between frontend and broker needs to handle CORS.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文