[英]Django: maintain sessions across multiple domains
我有两个/多个域,例如foo.com
和bar.com
,它们都具有相同的后端,这意味着两个域都将即将到来的请求重定向到托管在其他地方的同一个“Web 实例” 。
如果用户登录foo.com
,他/她还需要登录bar.com
才能访问任何端点/URL,例如bar.com/some/url/end-point/
。
如果我的域具有通用模式,则SESSION_COOKIE_DOMAIN
可能会做一些事情。 不幸的是,我没有。
题
如何跨多个域维护用户会话?
当您从安全角度来看时,这本身就是一种风险,任何解决方法的一个域都可以从另一个域读取 cookie。 所以出于显而易见的原因,这不能正常工作。
现在在大多数情况下,您唯一想要共享的是令牌或会话 ID。 所以你可以用不同的方式来解决这个问题
假设您的令牌是使用example.com/auth
生成的。 这个 url 可以返回 cookie 中的令牌以及 json 响应。 然后你也可以让这个 url 返回一个 301 到example.org/preauth?token=XXX
。 然后,此 url 将使用令牌设置 cookie
所以基本上,在这种情况下,您可以在服务器端处理整个方法
在这种情况下,您想要做的是拥有一个像素标签网址。 通过在example.com/auth
上执行 auth 收到 auth 令牌后
您将使用 javascript 在页面上动态添加一个图像源标签到您的另一个域
<img src='http://example.org/cookiepixel?token=yyy' />
这将返回将在example.org
而不是example.com
设置的 cookie
在这种方法中,您依赖于客户端代码来确保跨域身份验证发生。
我认为您无法跨完全不同的域进行单点登录。 但也许您可以使用OAuth 身份验证,两个域都指向同一个 OAuth 提供者? 然后实现一个 OAuth 提供程序,为任一域生成相同的访问令牌。 我不知道这可能需要多少努力。
这是个有趣的问题。 应该有很多方法可以做到,我想到的第一件事就是使用iframe
。 下面的例子是用Django 2.2
测试的。
在您的settings.py
,将您的sessionid
公开给 javascript。
SESSION_COOKIE_HTTPONLY = False
在你看来,一定要把xframe_options_exempt
放在上面,否则 django 将不允许它从另一个域“iframed”,这里我使用模板视图,所以我把装饰器放在urls.py
。
from django.views.decorators.clickjacking import xframe_options_exempt
urlpatterns = [
path(
'other_domain/',
xframe_options_exempt(TemplateView.as_view(template_name='examplesite/otherdomain.html')),
name='other_domain',
)
# ...
]
domains
是所有其他域的列表(不包括您的用户现在所在的域),在您的模板中,在<head>
标记中公开它们。
<head>
{{ domains|json_script:"domains" }}
{{ other_domain_path|json_script:"other-domain-path"}}
</head>
这将变成这样:
<script id="domains" type="application/json">["c222dbef.ngrok.io"] </script>
<script id="other-domain-path" type="application/json">"/other_domain/"</script>
然后在你的javascript中:
(function() {
function getCookie(cname) { //copied from w3schools
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function postSessionID(id) {
var domains = JSON.parse(document.getElementById("domains").textContent);
var path = JSON.parse(document.getElementById("other-domain-path").textContent);
domains.forEach(function(domain) {
var src = "https://" + domain + path;
var iframeEl = document.createElement("iframe");
iframeEl.setAttribute("class", "invisible");
iframeEl.setAttribute("src", src);
(function(id) { // this is an async call in a loop, create a closure here to protect "id"
iframeEl.addEventListener("load", function() {
this.contentWindow.postMessage(id, this.getAttribute("src"));
});
})(id);
document.body.appendChild(iframeEl);
});
}
function main() {
var sessionID = getCookie("sessionid");
if (!sessionID) {
return;
}
postSessionID(sessionID);
}
main();
})();
上面代码的想法是为其他域创建 iframe,iframe 的 src 指向我们名为“other_domain”的view
。 加载 iframe 后,我们使用postMessage
将会话 ID 发送给它们。
在examplesite/otherdomain.html
:
<head>
{{ domains|json_script:"domains" }}
{# we also need to expose all other domains #}
</head>
在你的脚本中:
(function() {
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
var domains = JSON.parse(document.getElementById("domains").textContent);
var trustedSources = domains.map(function(domain) {
return "https://" + domain;
});
window.addEventListener("message", function(e) {
if (!e.origin in trustedSources) {
return; // this prevents setting session id from other source
}
var sessionID = e.data;
// you can probably get your cookie expiry from your django view, so all of your cookie expires at the same time
setCookie("sessionid", sessionID, 365);
}, false);
})();
现在,您的用户可以从您的任何域登录和注销,并且他们将在您的所有域中进行相同的会话。
我在我的 github 中发布了完整的例子: https : //github.com/rabbit-aaron/django-multisite-sign-in
按照readme.md
进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.