微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 带有allowcredentials的通配符CORS

我有一个项目,我正在使用像user.mysite.com这样的通配符子域,其中username是通配符.

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

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

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

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

我理解这是出于安全原因,但现在我无法在我的应用程序中进行会话管理.有人可以为此建议解决方法吗?

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

$httpProvider.defaults.withCredentials = true;

我的ajax请求正被发送到不同的子域.即网址可能是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.您执行ajax调用的UI服务器是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代码所在的完整域名.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐