请求文件的 CORS 问题仅在 Google Chrome 上

问题描述

我有一个 .Net Core API 和一个使用 Babylonjs 来渲染一些 3D 模型的 React 应用程序。在我的服务器端,我存储了客户端将请求渲染到场景中的 3D 模型。为此,我在我的 .Net Core 服务器上允许 UseFileServerUseStaticFiles。我还创建了一个政策来申请需要 CORS 政策的请求。我实施的政策如下:

services.AddCors(options =>
        {
            options.AddPolicy("CorsPolicy",policyBuilder => policyBuilder
                    .AllowAnyHeader()
                    .SetIsOriginAllowedToAllowWildcardSubdomains()
                    .AllowAnyMethod()
                    .SetIsOriginAllowed((host) => true)
                    .AllowCredentials());
        });

我将其应用于以静态文件为目标的请求,方式如下:

app.UseFileServer(enableDirectorybrowsing: true);
app.UseStaticFiles(fileOptions);

在客户端,Babylon 库通过以下方式完成请求:

SceneLoader.ImportMeshAsync(
      "","http://localhost:9001/resources/objects/","sphere.glb",scene
    )
      .then((result) => {
        result.renderingGroupId = 1;
        scene.createDefaultCamera(true,true,true);
        scene.activeCamera.alpha += Math.PI;
      })
      .catch((error) => {
        return null;
      });

发生的情况是我仅在 google chrome 上收到以下错误,在 Firefox 和 Opera 上都没有

enter image description here

此外,对 Firefox 和 Opera 的响应包含缺少的标头,而对 chrome 的响应则没有。

铬:

enter image description here

火狐:

enter image description here

歌剧:

enter image description here

解决方法

经过一段时间的研究,我找到了解决问题的方法。导致问题的不是 CORS。看起来 Google 对 Chrome 进行了安全升级,将 Referrer-Policy 更改为:

enter image description here

wgile 其他两个浏览器都保持开启:

enter image description here enter image description here

因此,谷歌仅支持 https->https 站点上的 CORS。创建自签名证书并为 https 开放端口后问题已解决。