Access-Control-Allow-Origin http 标头有什么意义?

发布于 2024-12-29 00:26:22 字数 172 浏览 2 评论 0 原文

我很难理解 Access-Control-Allow-Origin http 标头的意义。

我认为如果客户端(浏览器)从服务器得到一次“否”,那么它就不会发送任何进一步的请求。但chrome和firefox不断发送请求。

谁能告诉我一个现实生活中的例子,这样的标题有意义吗?

谢谢!

I have difficulties in seeing the point of the Access-Control-Allow-Origin http header.

I thought that if a client (browser) gets a "no" from a server once, than it will not send any further requests. But chrome and firefox keep sending requests.

Could anyone tell me a real life example where a header like this makes sense?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2025-01-05 00:26:22

Access-Control-Allow-Origin 标头应包含“允许”访问资源的源列表。

因此,确定哪些域可以向您的服务器请求资源。

例如,发送回 Access-Control-Allow-Origin: * 标头将允许所有站点访问所请求的资源。

另一方面,发送回 Access-Control-Allow-Origin: http://foo.example.com 将仅允许访问 http://foo.example.com

Mozilla 开发者网站 上有更多相关信息

例如

假设我们自己的域中有一个 URL,它返回艺术家音乐专辑的 JSON 集合。它可能看起来像这样:

http://ourdomain.com/GetAlbumsByArtist/MichaelJackson.json

我们可能在我们的网站上使用一些 AJAX 来获取此 JSON 数据,然后将其显示在我们的网站上。

但是,如果其他站点的某人希望为自己使用我们的 JSON 对象怎么办?也许我们拥有另一个网站 http://subdomain.ourdomain.com,并且希望使用来自 ourdomain.com 的 Feed。

传统上我们无法对该数据发出跨域请求。

通过指定允许访问我们的资源的其他域,我们现在为跨域请求打开了大门。

The Access-Control-Allow-Origin header should contain a list of origins which are "allowed" to access the resource.

Thus, determining which domains can make requests to your server for resources.

For example, sending back a header of Access-Control-Allow-Origin: * would allow all sites to access the requested resource.

On the other hand, sending back Access-Control-Allow-Origin: http://foo.example.com will allow access only to http://foo.example.com.

There's some more information on this over at the Mozilla Developer Site

For example

Let's suppose we have a URL on our own domain that returns a JSON collection of Music Albums by Artist. It might look like this:

http://ourdomain.com/GetAlbumsByArtist/MichaelJackson.json

We might use some AJAX on our website to get this JSON data and then display it on our website.

But what if someone from another site wishes to use our JSON object for themselves? Perhaps we have another website http://subdomain.ourdomain.com which we own and would like to use our feed from ourdomain.com.

Traditionally we can't make cross-domain requests for this data.

By specifying other domains that are allowed access to our resource, we now open the doors to cross-domain requests.

入画浅相思 2025-01-05 00:26:22

CORS 实现了跨域的两部分安全视图。它试图解决的问题是,公共互联网上有许多服务器,这些服务器的编写者要么(a)假设没有浏览器允许跨源请求,要么(b)没有考虑过根本就没有。

因此,有些人希望允许跨源通信,但浏览器构建者并不认为他们可以解锁浏览器并突然让所有这些网站暴露出来。为了避免这种情况,他们发明了一种两部分结构。在浏览器允许与服务器进行跨域交互之前,该服务器必须明确表明它愿意允许跨域访问。在简单的情况下,这就是Access-Control-Allow-Origin。在更复杂的情况下,它是完整的预检机制。

服务器确实必须对其资源实施适当的资源访问控制。 CORS 只是允许服务器向浏览器表明它已了解所有问题。

CORS implements a two-part security view of cross-origin. The problem it is trying to solve is that there are many servers sitting out there on the public internet written by people who either (a) assumed that no browser would ever allow a cross-origin request, or (b) didn't think about it at all.

So, some people want to permit cross-origin communications, but the browser-builders do not feel that they can just unlock browsers and suddenly leave all these websites exposed. To avoid this, they invented a two-part structure. Before a browser will permit a cross-origin interaction with a server, that server has to specifically indicate that it is willing to allow cross-origin access. In the simple cases, that's Access-Control-Allow-Origin. In more complex cases, it's the full preflight mechanism.

It's still true that servers have to implement appropriate resource access control on their resources. CORS is just there to allow the server to indicate to browsers that it is aware of all the issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文