最近对接第三方网站出现一下错误:
Access to XMLHttpRequest at 'https://third.site.com/request_url' from origin 'https://main.site.com' 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.

根据错误线索查资料:

1.什么是 credentials
credentials,即用户凭证,是指 cookie、HTTP身份验证和TLS客户端证书。

XMLHttpRequest 的 withCredentials 属性:
默认值为false。在获取同域资源时设置 withCredentials 没有影响。
true:在跨域请求时,会携带用户凭证
false:在跨域请求时,不会携带用户凭证;返回的 response 里也会忽略 cookie

2.模拟:
$.ajax({
type: "POST",
url: 'https://third.site.com/request_url',
data: {'userid': '1112233', 'data': 'hello third'},
success: function(data){console.log(data)},
dataType: 'json',
xhrFields: {
withCredentials: true
}
});

Response headers:
Access-Control-Allow-Headers: origin, token
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Content-Type: application/json;charset=UTF-8
Date: Fri, 08 Mar 2019 10:11:21 GMT
Transfer-Encoding: chunked

查看console,提示文章开头的报错。

3.解决方法:
服务端返回的respoonse上加上:
response.setHeader("Access-Control-Allow-Origin", "https://main.site.com");
response.setHeader("Access-Control-Allow-Credentials", "true");
修改后,返回的请求头如下,浏览器不在报错:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: origin, token
Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE
Access-Control-Allow-Origin: https://main.site.com
Access-Control-Max-Age: 3600
Content-Type: application/json;charset=UTF-8
Date: Sat, 09 Mar 2019 06:22:19 GMT
Transfer-Encoding: chunked

4.想法: 从这个过程发现,如果可以将main.site.com站点的页面中注入类似的ajax请求,就可以将main.site.com的cookie发送到你想要的站点,完成用户凭证盗取,也就是CORS攻击。

最新文章

  1. 用VS Code写Python程序
  2. 深入浅出学Spring Data JPA
  3. 软件测试之loadrunner学习笔记-01事务
  4. 使用Gogs搭建Git服务器
  5. task mysqld:26208 blocked for more than 120 seconds
  6. .NET中的计时器控件Timer
  7. ExtJs之Ext.util.TaskRunner
  8. 实验一DOS报告
  9. jQuery限制文本框只能输入正整数
  10. C#调用SAPWebService
  11. 【自学php】第四天 - 使用数组
  12. COCOS2D-X FRAME动画创作随笔
  13. 用ifconfig命令,只有lo,没有eth0的解决方案
  14. 使用 after 伪类清除浮动
  15. 帧动画的创建方式 - 纯Java代码方式
  16. attr跟prop的区别:
  17. PhpStorm 2018 破解方法
  18. Dockerfile 构建前端node应用cnpm命令启动nodejs服务
  19. Java原子操作类汇总
  20. python作业模拟计算器开发(第五周)

热门文章

  1. AFNetworking 文件上传Data,File图片,文件等上传
  2. 安卓Eclipse开发人员的福音
  3. JMeter3.0配置
  4. JS中confirm,prompt用法
  5. AL2018届校招笔试——自然语言处理
  6. 【ASP.NET MVC系列】详解View
  7. 企业信息系统集成框架(设计思路)C模式
  8. Inno Setup 打包的文件以管理员权限执行
  9. 分享分享JavaScript100例-仅供参考
  10. 181213 - 解决Android的应用APP背景色突然被改变的问题