簡體   English   中英

Django,具有多個域的SESSION_COOKIE_DOMAIN

[英]Django, SESSION_COOKIE_DOMAIN with multiple domains

在 Django 中,我將 SESSION_COOKIE_DOMAIN 設置為我的域名。 但我實際上想用兩個不同的域名運行同一個站點。

設置 SESSION_COOKIE_DOMAIN 后,只有指定的域允許用戶登錄。 是否可以允許兩個域登錄?

如果您將會話 cookie 域設置為以“.”開頭字符它將讓您處理通配符子域並在多個子域之間共享會話 cookie(登錄會話)。

In settings.py:
SESSION_COOKIE_DOMAIN=".stackoverflow.com"

以上將允許 cookie 在 user1.stackoverflow.com 和 user2.stackoverflow.com 之間共享。

如果您確實希望同一站點的 url 不同,您是否希望同一用戶在一次登錄會話中在兩個站點之間切換? 或者你只是想讓兩個不同的用戶從兩個不同的 url(不是子域?)

標准的SessionMiddleware只支持一種SESSION_COOKIE_DOMAIN,只對一個域及其子域有效。

這是一個變體,它將根據請求主機動態設置 cookie 域。 要使用它,只需更新您的 MIDDLEWARE_CLASSES 以使用這個 SessionHostDomainMiddleware,而不是 SessionMiddleware。 這更好,@jcdyer 和@interstar?

import time

from django.conf import settings
from django.utils.cache import patch_vary_headers
from django.utils.http import cookie_date
from django.contrib.sessions.middleware import SessionMiddleware

class SessionHostDomainMiddleware(SessionMiddleware):
    def process_response(self, request, response):
        """
        If request.session was modified, or if the configuration is to save the
        session every time, save the changes and set a session cookie.
        """
        try:
            accessed = request.session.accessed
            modified = request.session.modified
        except AttributeError:
            pass
        else:
            if accessed:
                patch_vary_headers(response, ('Cookie',))
            if modified or settings.SESSION_SAVE_EVERY_REQUEST:
                if request.session.get_expire_at_browser_close():
                    max_age = None
                    expires = None
                else:
                    max_age = request.session.get_expiry_age()
                    expires_time = time.time() + max_age
                    expires = cookie_date(expires_time)
                # Save the session data and refresh the client cookie.
                # Skip session save for 500 responses, refs #3881.
                if response.status_code != 500:
                    request.session.save()
                    host = request.get_host().split(':')[0]
                    response.set_cookie(settings.SESSION_COOKIE_NAME,
                            request.session.session_key, max_age=max_age,
                            expires=expires, domain=host,
                            path=settings.SESSION_COOKIE_PATH,
                            secure=settings.SESSION_COOKIE_SECURE or None,
                            httponly=settings.SESSION_COOKIE_HTTPONLY or None)
        return response

我想這就是你要找的東西,我花了幾個小時才找到它

https://bitbucket.org/uysrc/django-dynamicsites

在 Django 中,我將 SESSION_COOKIE_DOMAIN 設置為我的域名。 但我實際上想用兩個不同的域名運行同一個站點。

設置 SESSION_COOKIE_DOMAIN 后,只有命名域允許用戶登錄。 是否可以允許兩個域都登錄?

我正在使用 django 3.1.4,它對我有用。

創建這樣的中間件,我在我的應用程序 utilities.middleware 中創建

class CrossDomainSessionMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        if response.cookies:
            host = request.get_host()
            # check if it's a different domain
            if host not in settings.SESSION_COOKIE_DOMAIN:
                domain = ".{domain}".format(domain=host)
                for cookie in response.cookies:
                    if 'domain' in response.cookies[cookie]:
                        response.cookies[cookie]['domain'] = domain
        return response

現在將此中間件放在 settings.py 中的 SessionMiddleware 上方

'utilities.middlware.CrossDomainSessionMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',

確保你的 settings.py 中有這兩個變量

SESSION_COOKIE_DOMAIN = '.domain.com'
SESSION_COOKIE_NAME = 'domainsessionid'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM