首页 > 解决方案 > CLI CORS 代理不更改来源,仍然在 API 请求上获得 403?

问题描述

是的,我发现了大量类似的东西,但我的实例仍未解决。我想避免杀死浏览器端的安全性,而是让代理完成它的工作,但我担心我在设置中遗漏了一些愚蠢的细节,而且我无论如何都不是 CORS 专家。

所以,我得到的是……一个 403;

Failed to load http://localhost:10000/some/rest/endpoint: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

其中localhost:10000是我的 api url,并且:4200是默认的 Angular CLI 实例。

所以我在 angular.json 中配置了我期望的代理;

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "angular-proj:build",
            "proxyConfig": "./proxy.conf.json"
          },

并添加一个proxy.conf.json;

{
    "/some/rest/*": {
      "target": "http://localhost:10000",
      "secure": false,
      "changeOrigin": true,
      "logLevel": "debug"
    }
  }

我提供它,甚至标记 CLI 服务确认的 proxy.conf;

[HPM] Proxy created: /some/rest -> http://localhost:10000ETC...

....除了我仍然得到 403,并且仍然在 Request* 标头中看到;

Host: localhost:10000
Origin: http://localhost:4200

所以我的问题是,我在这里遗漏了什么愚蠢的细节?我不想在所有来源的请求上加上星号,也不想关闭浏览器检查,我更希望将它很好地捆绑在服务配置中,就像我正在这样做一样另一对眼睛非常受欢迎。干杯

附录:我的 GET 似乎运行良好,但是类似于Access-Control-Request-Method: POST表演之类的东西Request Method: OPTIONS,这就是我得到 403 的地方……为什么 POST 会变成 OPTIONS?

附录#2:值得一提的是,我在 Chrome / Firefox 中遇到了这个问题,但在 Edge 中一切都是 kosher ???

附录#3:这是作为--aot=true代理运行的。

标签: javascriptangularcorsangular-cli

解决方案


以下标头是错误的,这就是您的浏览器仍然触发“同源策略”握手的原因:

Host: localhost:10000
Origin: http://localhost:4200

他们应该是:

Host: localhost:4200
Origin: http://localhost:4200

我坚信您没有将请求从http://localhost:10000/*to更改http://localhost:4200/some/rest/*,并且“大量类似的东西”不包括以下内容:Angular2 CORS issue on localhost


推荐阅读