ChatGPT解决这个技术问题 Extra ChatGPT

CORS:当凭据标志为真时,无法在 Access-Control-Allow-Origin 中使用通配符

我有一个涉及的设置

前端服务器(Node.js,域:localhost:3000)<---> 后端(Django,Ajax,域:localhost:8000)

浏览器 <-- webapp <-- Node.js(服务应用程序)

浏览器 (webapp) --> Ajax --> Django(服务 ajax POST 请求)

现在,我的问题是 webapp 用于对后端服务器进行 Ajax 调用的 CORS 设置。在 chrome 中,我不断得到

当凭证标志为真时,不能在 Access-Control-Allow-Origin 中使用通配符。

也不适用于Firefox。

我的 Node.js 设置是:

var allowCrossDomain = function(req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:8000/');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
};

在 Django 中,我使用的是 this middleware along with this

webapp 发出这样的请求:

$.ajax({
    type: "POST",
    url: 'http://localhost:8000/blah',
    data: {},
    xhrFields: {
        withCredentials: true
    },
    crossDomain: true,
    dataType: 'json',
    success: successHandler
});

因此,webapp 发送的请求标头如下所示:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"
Access-Control-Allow-Methods: 'GET,PUT,POST,DELETE'
Content-Type: application/json 
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: csrftoken=***; sessionid="***"

这是响应标头:

Access-Control-Allow-Headers: Content-Type,*
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Content-Type: application/json

我哪里错了?!

编辑 1:我一直在使用 chrome --disable-web-security,但现在希望事情能够真正发挥作用。

编辑 2: 答案:

所以,我的解决方案 django-cors-headers 配置:

CORS_ORIGIN_ALLOW_ALL = False
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = (
    'http://localhost:3000' # Here was the problem indeed and it has to be http://localhost:3000, not http://localhost:3000/
)
对我来说,它是没有 http 的 localhost:3000,如下所示:CORS_ORIGIN_WHITELIST = ('localhost:3000',)
你的意思是你在一台PC上使用开发前端和后端?
不同PC的前端和后端怎么样?
@ixaxaar 为什么你说 http 对你有用?我们都只有 ''localhost:3000'' 有效。
@244boy 是的,重点不是 http,而是最后的 /。我想省略 http 可能会起作用,但是我已经有几年没有真正研究过这个东西了,所以现在真的不知道什么有效!

C
Community

这是安全的一部分,你不能这样做。如果您想允许凭据,那么您的 Access-Control-Allow-Origin 不得使用 *。您必须指定确切的协议 + 域 + 端口。供参考,请参阅以下问题:

Access-Control-Allow-Origin 通配符子域、端口和协议 使用凭证的跨域资源共享

此外 * 过于宽松,会破坏凭据的使用。因此将 http://localhost:3000http://localhost:8000 设置为允许来源标头。


但是,如果有多个域怎么办?
@aroth您可以提供域列表。相关问题:stackoverflow.com/questions/1653308/…
@user568109 您能否解释一下“除了 * 过于宽松并且会破坏凭据的使用。”?
如果请求来自移动设备,那么“确切域”是什么,就像 Cordova 可能发生的那样?
@Christian 有点老了,但如果有人仍然好奇,这个问题只发生在浏览器上运行的应用程序上,因为出于安全原因,浏览器会抛出这个错误。其他客户端,例如移动应用程序、邮递员或任何其他使用 http 客户端发出请求的后端代码都不会出现此问题,因此您不必担心来源和确切的域。
B
BSMP

如果您正在使用 CORS 中间件并且想要发送 withCredential boolean true,则可以像这样配置 CORS:

var cors = require('cors');    
app.use(cors({credentials: true, origin: 'http://localhost:3000'}));

C
Christoph Hansen

扩展@Renaud 的想法,cors 现在提供了一种非常简单的方法:

从 cors 官方文档中找到 here

" origin:配置 Access-Control-Allow-Origin CORS 标头。可能的值:布尔值 - 将 origin 设置为 true 以反映请求来源,如 req.header('Origin') 所定义,或将其设置为 false 以禁用 CORS 。”

因此,我们只需执行以下操作:

const app = express();
const corsConfig = {
    credentials: true,
    origin: true,
};
app.use(cors(corsConfig));

最后,我认为值得一提的是,在某些用例中,我们希望允许来自任何人的跨源请求;例如,在构建公共 REST API 时。


我喜欢这个答案!特别是关于在某些用例中您希望允许所有来源的事实(这里的许多答案似乎都认为这始终是一种不好的做法)。
B
Bulkan

如果您使用 express,您可以使用 cors 包来允许这样的 CORS,而不是编写中间件;

var express = require('express')
, cors = require('cors')
, app = express();

app.use(cors());

app.get(function(req,res){ 
  res.send('hello');
});

啊,现在这样更方便了,但是结果是一样的:(顺便说一句,我使用的是 app.use(cors({credentials: true}));
您可能想要查看经过测试的 this Django CORS 中间件。
所以你有两个 Django 中间件?我只会使用 django-cors-header 应用程序。确保将 localhost 添加到 CORS_ORIGIN_WHITELIST 设置并将 CORS_ALLOW_CREDENTIALS 设置为 True
是的,伙计,之前尝试过但无济于事,有 CORS_ORIGIN_ALLOW_ALL = TrueCORS_ORIGIN_WHITELIST = ( 'localhost' )CORS_ALLOW_CREDENTIALS = True 我得到了这些标题:Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: http://localhost:3000/ Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE Content-Type: application/json
阅读本文档后:github.com/expressjs/corsuse 我使用此配置:app.use(cors({credentials: true, origin: 'localhost:3001'}));正在为我工作。
T
Thomas Smyth

试试看:

const cors = require('cors')

const corsOptions = {
    origin: 'http://localhost:4200',
    credentials: true,

}
app.use(cors(corsOptions));

S
Squirrl

如果您想允许所有来源并保持凭据真实,这对我有用:

app.use(cors({
  origin: function(origin, callback){
    return callback(null, true);
  },
  optionsSuccessStatus: 200,
  credentials: true
}));

@TSlegaitis 哈哈是的,这就是为什么它适用于所有来源但保留凭证的原因。出于安全考虑,我不推荐它,但它确实有效。
R
Renaud

这在开发中对我有用,但我不能建议在生产中,这只是完成工作的另一种方式,尚未提及但可能不是最好的。无论如何,这里是:

您可以从请求中获取来源,然后在响应标头中使用它。这是它在快递中的样子:

app.use(function(req, res, next) {
  res.header('Access-Control-Allow-Origin', req.header('origin') );
  next();
});

我不知道你的 python 设置会是什么样子,但这应该很容易翻译。


Mozilla Dev Docs 扩展将允许的来源更改为请求来源的想法。建议添加“Vary: Origin”HTTP 响应标头和允许的域白名单。
e
eriel marimon

(编辑)以前推荐的插件不再可用,您可以尝试 this other one

为了在 Chrome 中进行开发,安装 this add on 将消除该特定错误:

Access to XMLHttpRequest at 'http://192.168.1.42:8080/sockjs-node/info?t=1546163388687' 
from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 
'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' 
when the request's credentials mode is 'include'. The credentials mode of requests 
initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

安装后,请确保通过单击插件(CORS,绿色或红色)图标并填写相应的文本框,将您的 url 模式添加到 Intercepted URLs。要在此处添加的适用于 http://localhost:8080 的示例 URL 模式是:*://*


刚安装完就收到了,有什么办法吗?
它对我有用。警告如果您有其他类似的附加组件,您必须在尝试此之前将其卸载。
请修复损坏的链接
好像原来的插件被删除了,我在顶部添加了一个新的推荐作为(编辑)
c
connectedMind

在执行请求之前,使用 auth 拦截器编辑标头有这个问题。我们使用 api-token 进行身份验证,所以我启用了凭据。现在,似乎不再需要/允许

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    req = req.clone({
      //withCredentials: true, //not needed anymore
      setHeaders: {
        'Content-Type' : 'application/json',
        'API-TOKEN' : 'xxx'
      },
    });
    
    return next.handle(req);
  }

除此之外,目前没有任何副作用。