我知道只要它们是同一父域的子域,就可以允许其他域读取我们的域cookie .
例如,intranet.abc.com
并且extranet.abc.com
可以通过指定域属性来允许彼此读取cookie.abc.com
现在,我真的需要我可以允许其他域读取我的域cookie(它们不是同一域的子域).我在互联网上搜索了很多讨论=> 由于安全问题,所有人都说"不" .我不确定我是否错过了那里的解决方案,因为在这种情况下我没有看到任何安全问题.我的服务器明确允许XYZ.COM域读取此cookie,因为cookie不包含任何敏感信息,而XYZ.COM域是我的可信域,
在我看来,应该有一种方法来指定允许在我们的域中读取特定cookie的其他域的列表,就像CORS一样,服务器可以决定信息是否应该可用于某些可信域.
如果没有使用解决方法,请告诉我是否可行,如果是,该怎么做?如果不可能,我真的想知道原因.
有关我正在实施的内容的一些信息:
我正在实现文件下载,在客户端我需要通过使用javascript中的间隔定期检查cookie中的下载令牌来检测下载是否完成.
我目前正在处理的当前系统的逻辑可能将文件存储在2个不同的服务器中.如果当前服务器中缺少该文件,它将在另一个服务器(另一个域)中下载文件
非常感谢你.
您可以通过打开iframe到其他域上的特殊检测页面并使用window.postMessage API在窗口之间进行通信来读取域外cookie.显然只有HTML5.
为简洁起见,简化postMessage API,请参阅MDN开发人员页面以获取完整详细信息. https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage
<iframe id="ifrm" src="http://other.domain.com/getCookie.html"></iframe> <script> var iframe = document.getElementById('ifrm'); window.addEventListener('message', function (e) { if (e.source === iframe.contentWindow && e.origin === 'other.domain.com') { var cookie = e.data; //do something with cookie } }); //wait for the iframe to load...maybe ping it first...then iframe.contentWindow.postMessage('give me the cookie:cookie name', 'other.domain.com'); </script> /* in getCookie.html */ <script> window.addEventListener('message', function (e) { if (e.origin === 'your.domain.com') { var soughtCookie = /give me the cookie\:(.*)/.exec(e.data)[1]; // read the cookie var cookie = getCookieFn(soughtCookie) e.source.postMessage(cookie.toString(), 'your.domain.com'); } }, false); </script>