Django,具有多个域的 SESSION_COOKIE_DOMAIN
在 Django 中,我将 SESSION_COOKIE_DOMAIN 设置为我的域名。但我实际上想用两个不同的域名运行同一个网站。
设置 SESSION_COOKIE_DOMAIN 后,只有指定的域允许用户登录。是否可以同时允许两个域登录?
In Django, I have SESSION_COOKIE_DOMAIN set to my domain name. But I would actually like to run the same site with two different domain names.
With SESSION_COOKIE_DOMAIN set, only the named domain allows the user to login. Is it possible to allow both domains to login?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您将会话 cookie 域设置为以“.”开头字符它将让您处理通配符子域并在多个子域之间共享会话 cookie(登录会话)。
上面的代码将允许在 user1.stackoverflow.com 和 user2.stackoverflow.com 之间共享 cookie。
如果您确实希望同一站点的 url 不同,您是否希望同一用户在一次登录会话中在两个站点之间切换?或者您只是想让两个不同的用户从两个不同的网址(不是子域?)登录到该网站
If you set your session cookie domain to start with a "." character it will let you handle wildcard sub-domains and share a session cookie (login session) across multiple subdomains.
The above would allow a cookie to be shared across user1.stackoverflow.com and user2.stackoverflow.com.
If you really do want the url's to be different for the same site, would you want the same user to switch between the two sites on one login session? Or do you just want the ability to have two different users login to the site from two different url's (that are not sub-domains?)
标准SessionMiddleware仅支持1个SESSION_COOKIE_DOMAIN,仅适用于一个域及其子域。
这是一个变体,它将根据请求主机动态设置 cookie 域。要使用它,只需更新您的 MIDDLEWARE_CLASSES 以使用这一 SessionHostDomainMiddleware,而不是 SessionMiddleware。这更好吗,@jcdyer 和@interstar?
The standard SessionMiddleware only supports one SESSION_COOKIE_DOMAIN, which is only good for one domain and subdomains thereof.
Here's a variation that will set the cookie domain dynamically based on the request host. To use it, just update your MIDDLEWARE_CLASSES to use this one SessionHostDomainMiddleware, instead of SessionMiddleware. This better, @jcdyer and @interstar?
我想这就是你正在寻找的东西,我花了几个小时才找到它
https://bitbucket.org /uysrc/django-dynamicsites
I think this is what you are looking for, I took me hours to find it out
https://bitbucket.org/uysrc/django-dynamicsites
这是一个简单的 DynamicDomain 解决方案,适用于 Django 3.x、4.x 和 5.x。
设置
添加您想要支持的域。始终以“.”开头。支持所有子域。该列表可确保安全。没有人可以将他们的域名指向您的 IP 地址并获取会话 cookie!
在中间件中,添加新的中间件上面 SessionMiddleware
新会话中间件
创建新的中间件,负责根据传入请求正确设置 cookie 域。如果登录请求来自
abc.example.com
,则sessionid
的 cookiedomain
将设置为.example.com.同样,如果请求来自
abc.newexample.org
,它将被设置为.newexample.org
。Here's a simple DynamicDomain solution that works across Django 3.x, 4.x and 5.x.
Settings
Add the domains you want to support. Always start with a "." to support all subdomains. This list ensures security. No one can just point their domain to your IP address and get the session cookie delivered!
In middleware, add the new middleware above SessionMiddleware
New Session Middleware
Create the new middleware that takes care of setting cookie domain properly based on the incoming request. If request to login comes from
abc.example.com
the cookiedomain
forsessionid
will be set to.example.com
. Similarly, if request comes fromabc.newexample.org
it will be set to.newexample.org
.我正在使用 django 3.1.4,它对我有用。
创建一个像这样的中间件,我正在我的应用程序中创建utilities.middleware
现在将此中间件放在settings.py内的SessionMiddleware上方
确保您的settings.py中有这两个变量
I am using django 3.1.4, it worked for me.
Create a middleware like this, I am creating inside my app utilities.middleware
Now place this middleware above SessionMiddleware inside settings.py
Make sure you have these two variable in your settings.py