【发布时间】:2021-07-23 14:32:08
【问题描述】:
我对 CORS 提供的安全性有点困惑。下面是两个几乎相同的 HTTP 请求,一个有效,一个无效,一个是通过 curl 另一个是浏览器中的 javascript。
终端
$ curl https://www.google.com/
--> Returns a page
浏览器:
// Open the console in the browser (or spin put localhost)
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.google.com");
xhr.send();
--> CORS Error
再试一次:
const xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.google.com");
xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
xhr.setRequestHeader("Access-Control-Allow-Methods", 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
xhr.setRequestHeader("Access-Control-Allow-Headers", 'Origin,Authorization,Content-Type,X-Auth-Token');
xhr.setRequestHeader("Access-Control-Allow-Credentials", 'true')
xhr.send();
--> CORS Error still
所以我猜测 google.com 服务器已将其设置为仅接受来自 google 域的请求。但是,当我从不属于谷歌域的终端卷曲时,我会收到带有 HTML 等的 200 响应。
那么为什么服务器会响应我没有域的终端,而当我在浏览器中使用javascript时没有响应?
谢谢^.^
【问题讨论】:
-
How much research effort is expected of Stack Overflow users? “在 Stack Overflow 上提问应该是你寻找答案过程中的最后一步” 使用我的搜索引擎快速搜索“CORS”给出了我在第一个结果的第一段中给出答案。
标签: javascript networking xmlhttprequest cors network-security