这是一个有趣的问题。应该有很多方法,我首先想到的是使用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进行设置。