为什么我调用第三方服务时不强制执行No 'Access-Control-Allow-Origin',而调用我自己的服务器时却强制执行?
Why does No 'Access-Control-Allow-Origin' is not enforced when I call third party services, but it is when I call my own server?
我正在 JAX-RS
服务器上构建一个 RESTful
服务,一些客户端将附加到它。
开始在客户端上测试端点的时间到了,我首先在 JavaScript
上进行了尝试,因为到目前为止,我可以很容易地使用以下代码向第三方资源发出请求:
function httpGet(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
我知道我不应该做同步请求,但那是题外话。
在 Firefox 上,我得到的错误是:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://someurl.com/someresource/. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
这些请求在我的本地服务器和部署服务器上都不起作用。
我发现这个问题的大多数解决方案都必须通过设置 header Access-Control-Allow-Origin: *
来解决。我试过了,但对我没用。
起初我以为是我的服务器配置有问题,但现在我认为是浏览器因为同源策略不让我执行请求。这样对吗?如果它是正确的,为什么与上面完全相同的代码,没有 Access-Control-Allow-Headers: *
,适用于第三方服务(Google、Facebook 等)?
是否有始终允许违反同源策略的网站白名单?
如果最后一个问题的答案是否定的,那么他们必须在他们的服务器端代码上进行一些特定的配置以允许跨源通信发生。这个配置可能是什么?
At first I thought it was a problem with my server configuration, but now I think it's the browser that is not letting me execute the request because of the Same Origin Policy. Is this correct?
通过默认,浏览器将强制执行同源策略并阻止您的JavaScript访问数据。
您向其发出请求的服务器的配置可以设置 CORS headers(包括 Access-Control-Allow-Origin
和 Access-Control-Allow-Headers
)以告诉浏览器 not 为该请求执行同源策略。
Is there a whitelist of sites that are always allowed to break the Same Origin Policy?
没有
If the answer to the last question is no, then they must have some specific configutation on their server side code to allow Cross Origin communications to happen. What could this configuration be?
配置设置了您引用的错误消息中描述的响应 headers。
At first I thought it was a problem with my server configuration
是。
but now I think it's the browser that is not letting me execute the request because of the Same Origin Policy.
没错。浏览器根据 SOP 不允许请求,因为 您的服务器未配置为允许 cross-origin 请求。
不仅仅是传回一个 header。 Full details in the spec,但基本上可以归结为:
响应 OPTIONS
请求,而不仅仅是 GET
、POST
等。
用 所有 必要的 header 回复。
用 header 的正确值响应。
您必须发回的 header 至少:
Access-Control-Allow-Origin
Access-Control-Allow-Methods
Access-Control-Allow-Headers
您可能还需要 Access-Control-Allow-Credentials
。您需要为这些提供的值通常是从 header 中派生出来的,这些 header 具有与请求相关的相似(但略有不同)的名称。
您必须提供 header 以响应 OPTIONS
调用(如果有),以及随后的 GET
或 POST
等。打电话。
If it's correct, why does the exact same code as above, with no Access-Control-Allow-Headers: *, works for third party services (Google, Facebook, etc.)?
浏览器自动填写请求header秒;不同之处在于 服务器 响应请求。
Is there a whitelist of sites that are always allowed to break the Same Origin Policy?
不,当然不是。
If the answer to the last question is no, then they must have some specific configutation on their server side code to allow Cross Origin communications to happen. What could this configuration be?
以上。
这是 服务器 端通过 CORS 授予访问权限的一些伪代码(它是用 JavaScript 编写的,因为我知道您熟悉 JavaScript,但它 是 伪代码,您需要在您的服务器上执行此操作):
var origin, method, headers;
origin = getRequestHeader("Origin");
if (origin /* and you want to grant access to it */) {
addResponseHeader("Access-Control-Allow-Origin", origin);
method = getRequestHeader("Access-Control-Request-Method");
if (method) {
// Note the request header is singular, but the response header is plural
addResponseHeader("Access-Control-Allow-Methods", method);
}
headers = getRequestHeader("Access-Control-Request-Headers");
if (headers) {
addResponseHeader("Access-Control-Allow-Headers", headers);
}
if (/* You want to allow the origin to provide credentials and cookies*/) {
addResponseHeader("Access-Control-Allow-Credentials", "true");
}
}
我正在 JAX-RS
服务器上构建一个 RESTful
服务,一些客户端将附加到它。
开始在客户端上测试端点的时间到了,我首先在 JavaScript
上进行了尝试,因为到目前为止,我可以很容易地使用以下代码向第三方资源发出请求:
function httpGet(theUrl){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false );
xmlHttp.send( null );
return xmlHttp.responseText;
}
我知道我不应该做同步请求,但那是题外话。
在 Firefox 上,我得到的错误是:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://someurl.com/someresource/. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
这些请求在我的本地服务器和部署服务器上都不起作用。
我发现这个问题的大多数解决方案都必须通过设置 header Access-Control-Allow-Origin: *
来解决。我试过了,但对我没用。
起初我以为是我的服务器配置有问题,但现在我认为是浏览器因为同源策略不让我执行请求。这样对吗?如果它是正确的,为什么与上面完全相同的代码,没有 Access-Control-Allow-Headers: *
,适用于第三方服务(Google、Facebook 等)?
是否有始终允许违反同源策略的网站白名单?
如果最后一个问题的答案是否定的,那么他们必须在他们的服务器端代码上进行一些特定的配置以允许跨源通信发生。这个配置可能是什么?
At first I thought it was a problem with my server configuration, but now I think it's the browser that is not letting me execute the request because of the Same Origin Policy. Is this correct?
通过默认,浏览器将强制执行同源策略并阻止您的JavaScript访问数据。
您向其发出请求的服务器的配置可以设置 CORS headers(包括 Access-Control-Allow-Origin
和 Access-Control-Allow-Headers
)以告诉浏览器 not 为该请求执行同源策略。
Is there a whitelist of sites that are always allowed to break the Same Origin Policy?
没有
If the answer to the last question is no, then they must have some specific configutation on their server side code to allow Cross Origin communications to happen. What could this configuration be?
配置设置了您引用的错误消息中描述的响应 headers。
At first I thought it was a problem with my server configuration
是。
but now I think it's the browser that is not letting me execute the request because of the Same Origin Policy.
没错。浏览器根据 SOP 不允许请求,因为 您的服务器未配置为允许 cross-origin 请求。
不仅仅是传回一个 header。 Full details in the spec,但基本上可以归结为:
响应
OPTIONS
请求,而不仅仅是GET
、POST
等。用 所有 必要的 header 回复。
用 header 的正确值响应。
您必须发回的 header 至少:
Access-Control-Allow-Origin
Access-Control-Allow-Methods
Access-Control-Allow-Headers
您可能还需要 Access-Control-Allow-Credentials
。您需要为这些提供的值通常是从 header 中派生出来的,这些 header 具有与请求相关的相似(但略有不同)的名称。
您必须提供 header 以响应 OPTIONS
调用(如果有),以及随后的 GET
或 POST
等。打电话。
If it's correct, why does the exact same code as above, with no Access-Control-Allow-Headers: *, works for third party services (Google, Facebook, etc.)?
浏览器自动填写请求header秒;不同之处在于 服务器 响应请求。
Is there a whitelist of sites that are always allowed to break the Same Origin Policy?
不,当然不是。
If the answer to the last question is no, then they must have some specific configutation on their server side code to allow Cross Origin communications to happen. What could this configuration be?
以上。
这是 服务器 端通过 CORS 授予访问权限的一些伪代码(它是用 JavaScript 编写的,因为我知道您熟悉 JavaScript,但它 是 伪代码,您需要在您的服务器上执行此操作):
var origin, method, headers;
origin = getRequestHeader("Origin");
if (origin /* and you want to grant access to it */) {
addResponseHeader("Access-Control-Allow-Origin", origin);
method = getRequestHeader("Access-Control-Request-Method");
if (method) {
// Note the request header is singular, but the response header is plural
addResponseHeader("Access-Control-Allow-Methods", method);
}
headers = getRequestHeader("Access-Control-Request-Headers");
if (headers) {
addResponseHeader("Access-Control-Allow-Headers", headers);
}
if (/* You want to allow the origin to provide credentials and cookies*/) {
addResponseHeader("Access-Control-Allow-Credentials", "true");
}
}