Kha*_* TO 2 javascript cookies web-applications
我知道只要它们是同一父域的子域,就可以允许其他域读取我们的域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>
Run Code Online (Sandbox Code Playgroud)
您可以有一个后端 Web 服务,它与第 3 方共享 cookie 的内容,但是您的服务器必须在会话中保存 cookie 值,并拥有一个与其他网站共享的会话 ID。
还可以使用特殊页面和重定向,以便读取 cookie 值并将其作为表单提交传递到您的域。
假设您的域名是 yours.com,并且在页面 yours.com/page1 上设置了一些 cookie 值。
现在 xyz.com ,另一个域需要该值。xyz.com/somePage,重定向到 yours.com/spl(以及发送用户说 xyz.com/somePage2 的页面参数),现在 yours.com/spl 通过 JavaScript 获取 cookie,然后重定向到 xyz.com /somePage2 将 cookie 值作为 POST 或 GET 参数传递。
完整的工作示例位于http://sel2in.com/pages/prog/html/acrossSites/make.php(带有简单的Web服务)
AJAX 不是示例无法工作,但可以使用 iframe 来完成。
代码 :
coki.js(进入第一个想要公开 cookie 的网站)
function setCookie(cname,cvalue, daysExpire)
{
var d = new Date();
d.setTime(d.getTime()+(daysExpire * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires + " ; path=/ ;"
}
function getCookie(cname)
{
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++)
{
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length,c.length);
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
wsa.php(位于站点 1)。为了使其更安全,可以检查调用页面/容器 URL 并使用动态密钥。
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
error_reporting(E_WARNING);
$d = $_REQUEST['s'];
if($d != "secret565"){
echo "Bad secret bye";
return;
}
$n = $_REQUEST['n'];
if($n == ""){
echo "No cookie name, bye";
return;
}
?>
<script src=coki.js>
</script>
<script >
n = '<?php echo "$n"?>'
v = getCookie(n)
//alert("For " + n + ", got :" + v + ".")
window.parent.gotVal(n, v)
</script>
Run Code Online (Sandbox Code Playgroud)
getc.html
访问站点 2,使用 iframe 通过 wsa.php 从站点 1 获取 cookie C1 或其他 cookie 的值。wsa.php 从其参数中读取秘密身份验证密钥和 cookie 名称,然后调用包含页面中的 javascript 函数来传回值
<form name=f1 action=ws.php method=post>
<h1>Get cookie from Javascript sample </h1>
http://sel2in.com/pages/prog/html/acrossSites/
<table>
<tr><td>Url from <td/><td> <input name=u1 value='wsa.php' size=100><td/></tr>
<tr><td>Cookie Name <td/><td> <input name=n value='C1'><td/></tr>
<tr><td>Secret <td/><td> <input name=s value='secret565'><td/></tr>
<tr><td><input type=button value='Go' onclick='s1do()' > <td/><td><td/></tr>
</table>
</form>
<div id = result>result here</div>
<div id = cc1>container</div>
v 2 c
<script>
function gotVal(n, v){
document.getElementById("result").innerHTML = "For " + n + ", got :" + v + "."
}
function s1do(){
document.getElementById("cc1").innerHTML = ""
n1 = document.f1.n.value
s1 = document.f1.s.value
url = document.f1.u1.value
qry = "s=" + escape(s1) + "&n=" + escape(n1)
s = "<iframe border=0 height =1 width=1 src=\"" + url + "?" + qry + "\" ></iframe>"
document.getElementById("cc1").innerHTML = s
}
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3140 次 |
| 最近记录: |