我有两个WebApp1和WebApp2在两个不同的域。
我在WebApp1的HttpResponse中设置了一个cookie。 如何从WebApp2中的HttpRequest读取相同的cookie ?
我知道这听起来很奇怪,因为cookie是特定于给定域的,我们不能从不同的域访问它们;不过我听说过跨域cookie,它可以在多个web应用程序之间共享。如何使用跨域cookie实现这一需求?
注意:我正在尝试使用J2EE web应用程序
我有两个WebApp1和WebApp2在两个不同的域。
我在WebApp1的HttpResponse中设置了一个cookie。 如何从WebApp2中的HttpRequest读取相同的cookie ?
我知道这听起来很奇怪,因为cookie是特定于给定域的,我们不能从不同的域访问它们;不过我听说过跨域cookie,它可以在多个web应用程序之间共享。如何使用跨域cookie实现这一需求?
注意:我正在尝试使用J2EE web应用程序
当前回答
据我所知,cookie受到“同源”策略的限制。但是,使用CORS,您可以接收和使用“服务器B”cookie,在“服务器B”上从“服务器a”建立持久会话。
尽管,这需要在“服务器B”上设置一些头文件:
Access-Control-Allow-Origin: http://server-a.example.com
Access-Control-Allow-Credentials: true
你将需要在所有的“服务器A”请求(例如:xhr)上发送“withCredentials”标志。withCredentials = true;)
你可以在这里阅读:
http://www.html5rocks.com/en/tutorials/cors/
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
其他回答
最聪明的解决办法就是效仿facebook的做法。当你访问任何域名时,facebook如何知道你是谁?其实很简单:
The Like button actually allows Facebook to track all visitors of the external site, no matter if they click it or not. Facebook can do that because they use an iframe to display the button. An iframe is something like an embedded browser window within a page. The difference between using an iframe and a simple image for the button is that the iframe contains a complete web page – from Facebook. There is not much going on on this page, except for the button and the information about how many people have liked the current page.
所以当你在cnn.com上看到一个“喜欢”按钮时,你实际上是在同时访问一个Facebook页面。这使得Facebook可以读取你电脑上的cookie,这些cookie是它在你上次登录Facebook时创建的。
每种浏览器的一个基本安全规则是,只有创建了cookie的网站以后才能读取它。这就是iframe的优势:它允许Facebook读取你的Facebook-cookie,即使你在访问另一个网站。这就是他们如何在cnn.com上认出你,并在那里展示你的朋友。
来源:
http://dorianroy.com/blog/2010/04/how-facebooks-like-button-works/ https://stackoverflow.com/a/8256920/715483
据我所知,cookie受到“同源”策略的限制。但是,使用CORS,您可以接收和使用“服务器B”cookie,在“服务器B”上从“服务器a”建立持久会话。
尽管,这需要在“服务器B”上设置一些头文件:
Access-Control-Allow-Origin: http://server-a.example.com
Access-Control-Allow-Credentials: true
你将需要在所有的“服务器A”请求(例如:xhr)上发送“withCredentials”标志。withCredentials = true;)
你可以在这里阅读:
http://www.html5rocks.com/en/tutorials/cors/
https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS
在nfriedly.com上有一个关于Facebook如何做到这一点的不错的概述
还有浏览器指纹识别,它与cookie不同,但作用类似,因为它可以帮助您以相当程度的确定性识别用户。Stack Overflow上有一篇文章提到了一种指纹识别方法
function GetOrder(status, filter) {
var isValid = true; //isValidGuid(customerId);
if (isValid) {
var refundhtmlstr = '';
//varsURL = ApiPath + '/api/Orders/Customer/' + customerId + '?status=' + status + '&filter=' + filter;
varsURL = ApiPath + '/api/Orders/Customer?status=' + status + '&filter=' + filter;
$.ajax({
type: "GET",
//url: ApiPath + '/api/Orders/Customer/' + customerId + '?status=' + status + '&filter=' + filter,
url: ApiPath + '/api/Orders/Customer?status=' + status + '&filter=' + filter,
dataType: "json",
crossDomain: true,
xhrFields: {
withCredentials: true
},
success: function (data) {
var htmlStr = '';
if (data == null || data.Count === 0) {
htmlStr = '<div class="card"><div class="card-header">Bu kriterlere uygun sipariş bulunamadı.</div></div>';
}
else {
$('#ReturnPolicyBtnUrl').attr('href', data.ReturnPolicyBtnUrl);
var groupedData = data.OrderDto.sort(function (x, y) {
return new Date(y.OrderDate) - new Date(x.OrderDate);
});
groupedData = _.groupBy(data.OrderDto, function (d) { return toMonthStr(d.OrderDate) });
localStorage['orderData'] = JSON.stringify(data.OrderDto);
$.each(groupedData, function (key, val) {
var sortedData = groupedData[key].sort(function (x, y) {
return new Date(y.OrderDate) - new Date(x.OrderDate);
});
htmlStr += '<div class="card-header">' + key + '</div>';
$.each(sortedData, function (keyitem, valitem) {
//Date Convertions
if (valitem.StatusDesc != null) {
valitem.StatusDesc = valitem.StatusDesc;
}
var date = valitem.OrderDate;
date = date.substring(0, 10).split('-');
date = date[2] + '.' + date[1] + '.' + date[0];
htmlStr += '<div class="col-lg-12 col-md-12 col-xs-12 col-sm-12 card-item clearfix ">' +
//'<div class="card-item-head"><span class="order-head">Sipariş No: <a href="ViewOrderDetails.html?CustomerId=' + customerId + '&OrderNo=' + valitem.OrderNumber + '" >' + valitem.OrderNumber + '</a></span><span class="order-date">' + date + '</span></div>' +
'<div class="card-item-head"><span class="order-head">Sipariş No: <a href="ViewOrderDetails.html?OrderNo=' + valitem.OrderNumber + '" >' + valitem.OrderNumber + '</a></span><span class="order-date">' + date + '</span></div>' +
'<div class="card-item-head-desc">' + valitem.StatusDesc + '</div>' +
'<div class="card-item-body">' +
'<div class="slider responsive">';
var i = 0;
$.each(valitem.ItemList, function (keylineitem, vallineitem) {
var imageUrl = vallineitem.ProductImageUrl.replace('{size}', 200);
htmlStr += '<div><img src="' + imageUrl + '" alt="' + vallineitem.ProductName + '"><span class="img-desc">' + ProductNameStr(vallineitem.ProductName) + '</span></div>';
i++;
});
htmlStr += '</div>' +
'</div>' +
'</div>';
});
});
$.each(data.OrderDto, function (key, value) {
if (value.IsSAPMigrationflag === true) {
refundhtmlstr = '<div class="notify-reason"><span class="note"><B>Notification : </B> Geçmiş siparişleriniz yükleniyor. Lütfen kısa bir süre sonra tekrar kontrol ediniz. Teşekkürler. </span></div>';
}
});
}
$('#orders').html(htmlStr);
$("#notification").html(refundhtmlstr);
ApplySlide();
},
error: function () {
console.log("System Failure");
}
});
}
}
. config
包括UI原点和设置允许凭据为真
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://burada.com" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
</customHeaders>
</httpProtocol>
就像其他人说的,你不能分享cookie,但你可以这样做:
将所有cookie集中到一个域中,例如cookemaker .example 当用户向example.com发出请求时,您将他重定向到cookiemaker.example cookiemaker。Example将他重定向到example.com,并提供你需要的信息
当然,它不是完全安全的,你必须在你的应用程序之间创建某种内部协议来做到这一点。
最后,如果在每个请求中都这样做,对用户来说会非常讨厌,但如果只是第一个请求就不会。
但我认为没有别的办法了。