我有两个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应用程序
当前回答
我已经创建了一个NPM模块,它允许你跨域共享本地存储的数据: https://www.npmjs.com/package/cookie-toss
通过使用托管在域A上的iframe,您可以将所有用户数据存储在域A上,并通过向域A iframe发送请求来引用该数据。
因此,域B、域C等可以注入iframe并向其发送请求,以存储和访问所需的数据。域A成为所有共享数据的中心。
使用域a中的域白名单,您可以确保只有您依赖的站点可以访问域a上的数据。
诀窍是在域A上的iframe中有能够识别正在请求的数据的代码。上面NPM模块中的README更深入地介绍了这个过程。
希望这能有所帮助!
其他回答
您可以尝试使用图像标记将cookie val推到另一个域。
在尝试这样做时,您的情况可能会有所不同,因为一些浏览器要求您在WebApp2域上有适当的P3P策略,否则浏览器将拒绝cookie。
如果你看plus.google.com p3p政策,你会看到他们的政策是:
CP="这不是P3P策略!更多信息请访问http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657。”
这就是他们对这些跨域请求使用的+1按钮的策略。
另一个警告是,如果您使用https,请确保图像标签指向https地址,否则cookie将不会设置。
据我所知,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
阅读Web Api中的Cookie
var cookie = actionContext.Request.Headers.GetCookies("newhbsslv1");
Logger.Log("Cookie " + cookie, LoggerLevel.Info);
Logger.Log("Cookie count " + cookie.Count, LoggerLevel.Info);
if (cookie != null && cookie.Count > 0)
{
Logger.Log("Befor For " , LoggerLevel.Info);
foreach (var perCookie in cookie[0].Cookies)
{
Logger.Log("perCookie " + perCookie, LoggerLevel.Info);
if (perCookie.Name == "newhbsslv1")
{
strToken = perCookie.Value;
}
}
}
基于浏览器的存储主要有三种:
会话存储 本地存储 cookie存储
安全cookie -用于加密网站,以提供保护,免受来自黑客的任何可能威胁。 访问cookie - document.cookie。这意味着这个cookie是公开的,可以通过跨站点脚本来利用。保存的cookie值可以通过浏览器控制台看到。
作为预防措施,您应该总是尝试使用JavaScript使您的cookie在客户端不可访问。
HTTPonly - ensures that a cookie is not accessible using the JavaScript code. This is the most crucial form of protection against cross-scripting attacks. A secure attribute - ensures that the browser will reject cookies unless the connection happens over HTTPS. sameSite attribute improves cookie security and avoids privacy leaks. sameSite=Lax - It is set to Lax (sameSite = Lax) meaning a cookie is only set when the domain in the URL of the browser matches the domain of the cookie, thus eliminating third party’s domains. This will restrict cross-site sharing even between different domains that the same publisher owns. we need to include SameSite=None to avoid the new default of Lax:
注意:有一个规范草案要求当SameSite属性被设置为“none”时Secure属性被设置为true。一些web浏览器或其他客户端可能采用此规范。
使用include作为{withCredentials: true}必须包含来自前端的请求的所有cookie。
const data = { email: 'youremailaddress@gmail.com' , password: '1234' };
const response = await axios.post('www.yourapi.com/login', data , { withCredentials: true });
Cookie只能在安全的HTTPS连接上被接受。为了使其工作,我们必须将web应用程序移动到HTTPS。
在express.js
res.cookie('token', token, {
maxAge: 1000 * 60 * 60 * 24, // would expire after (for 15 minutes 1000 * 60 * 15 ) 15 minutes
httpOnly: true, // The cookie only accessible by the web server
sameSite: 'none',
secure: true, // Marks the cookie to be used with HTTPS only.
});
参考文献1,参考文献2
在nfriedly.com上有一个关于Facebook如何做到这一点的不错的概述
还有浏览器指纹识别,它与cookie不同,但作用类似,因为它可以帮助您以相当程度的确定性识别用户。Stack Overflow上有一篇文章提到了一种指纹识别方法