gpt4 book ai didi

php - 带有 allowcredentials true 的通配符 CORS

转载 作者:可可西里 更新时间:2023-10-31 22:43:15 24 4
gpt4 key购买 nike

我有一个项目,我在其中使用通配符子域,例如 user.mysite.com,其中用户名是通配符。

在我的服务器 (PHP) 中,我设置了以下 header :

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');

这允许我从前端执行 ajax 调用,因为我不知道域是什么。但是,我还需要将我的 cookie 与维护 session 的请求一起发送,但是当我告诉前端的 AngularJS 发送凭据时,我收到以下消息:

A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true

我知道这是出于安全原因,但现在我无法在我的应用程序中进行 session 管理。任何人都可以为此提出解决方法吗?

我告诉 Angular 发送凭据的地方:

$httpProvider.defaults.withCredentials = true;

我的 ajax 请求被发送到不同的子域。即 url 可以是 billy.mysite.com 但 ajax 请求被发送到 api.mysite.com

最佳答案

@iamjonesy - 你犯了愚蠢的错误。

"A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true" 

要避免上述错误,请按照以下步骤操作。假设你的 api 服务器是 http://api.com 。而你的 UI 服务器,从你执行 ajax 调用的地方,是 http://ui.com

PHP 头代码应该是这样的,

header('Access-Control-Allow-Origin: http://ui.com');
header('Access-Control-Allow-Credentials: true');

将下面这行放在angular config.js中

$httpProvider.defaults.withCredentials = true;

我所做的,从未将“*”设置为 $httpProvider.defaults.withCredentials 的通配符,只是提及 ajax 代码所在的完整域名。

关于php - 带有 allowcredentials true 的通配符 CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24531864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com