【问题标题】:django different sessions on different tabs (unknown amount of different sessions)django 不同选项卡上的不同会话(未知数量的不同会话)
【发布时间】:2015-09-23 16:19:54
【问题描述】:

我正在尝试创建一个 django 应用程序,其中同一用户可以在同一浏览器上打开两个选项卡并作为两个不同的会话登录,最好仍然使用 cookie。

一些背景:我的应用程序是为其他网站提供的服务,用户从客户网站被引用到我的网站。所以用户进入我的域,但由于他们来自不同的站点,所以他们的帐户不能混合。

注意:如果有帮助,我可以将用户重定向到每个客户端站点的唯一 URL。

我应该如何解决这个问题?有没有现成的解决方案?

谢谢!

【问题讨论】:

  • 不确定这是否可以实现,因为会话可能存储在 cookie 中,这取决于您的浏览器所呈现的 cookie
  • 如果您在不同的三级域(例如subdomain1.mydomain.comsubdomain2.mydomain.com
  • @mastazi 这是我正在考虑的一般方向之一,我不喜欢它是需要为使用我的服务的每个客户创建 dns 记录(例如,如果“somesite.com”是使用我的服务,我必须为 somesite.mydomain.com 创建一条记录并在我的服务器上接受它)。有没有办法在不实际使用不同域的情况下伪造这种行为?你可能知道的任何技巧?谢谢
  • @Ness 如果您想实现该工作流程,那么您应该实现自己的会话管理系统并忽略 django-sessions。相关问题:stackoverflow.com/questions/3862888/…
  • @Ness 我支持 AdolAurion 提出的解决方案,考虑到您所说的,您可能应该创建自己的会话。我知道这需要更多工作(并且可能某些外部库可能无法正常工作),但它是最灵活的解决方案。

标签: django session django-socialauth django-sessions


【解决方案1】:

我最终使用了来自this question 的解决方案,AdolAurion 在 cmets 中链接。注意:我的会话与那里的解决方案有点不同:

import time
from importlib import import_module

from django.conf import settings
from django.utils.cache import patch_vary_headers
from django.utils.http import cookie_date


class SessionMiddleware(object):
    '''
    session middleware that support multiple sessions based on url
    '''

    def __init__(self):
        engine = import_module(settings.SESSION_ENGINE)
        self.SessionStore = engine.SessionStore

    def get_cookie_name(self, request):
        '''
        get session cookie name based on url
        '''
        return "sessionid_" + request.path.split("/")[1]

    def process_request(self, request):
        session_key = request.COOKIES.get(self.get_cookie_name(request), None)
        request.session = self.SessionStore(session_key)

    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 or delete
        the session cookie if the session has been emptied.
        """
        try:
            accessed = request.session.accessed
            modified = request.session.modified
            empty = request.session.is_empty()
        except AttributeError:
            pass
        else:
            # First check if we need to delete this cookie.
            # The session should be deleted only if the session is entirely empty
            if self.get_cookie_name(request) in request.COOKIES and empty:
                response.delete_cookie(self.get_cookie_name(request),
                    domain=settings.SESSION_COOKIE_DOMAIN)
            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()
                        response.set_cookie(self.get_cookie_name(request),
                                request.session.session_key, max_age=max_age,
                                expires=expires, domain=settings.SESSION_COOKIE_DOMAIN,
                                path=settings.SESSION_COOKIE_PATH,
                                secure=settings.SESSION_COOKIE_SECURE or None,
                                httponly=settings.SESSION_COOKIE_HTTPONLY or None)
        return response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2014-05-19
    • 2011-02-19
    • 1970-01-01
    • 2012-04-28
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多