目前,我的烧瓶应用程序(使用会话)执行以下操作来处理ONE域:
app.config.from_object(设置)
并在设置对象中:
SESSION_COOKIE_DOMAIN =".firstirst"
我现在要做的是动态设置会话cookie域来处理,例如来自www.first.com和www.second.com的请求.请注意,我说的是域名,但不是子域名.谢谢.
Grepping SESSION_COOKIE_DOMAIN
通过瓶的GitHub库可以看出,它是用来像这样:
def get_cookie_domain(self, app):
"""Helpful helper method that returns the cookie domain that should
be used for the session cookie if session cookies are used.
"""
if app.config['SESSION_COOKIE_DOMAIN'] is not None:
return app.config['SESSION_COOKIE_DOMAIN']
if app.config['SERVER_NAME'] is not None:
# chop of the port which is usually not supported by browsers
rv = '.' + app.config['SERVER_NAME'].rsplit(':', 1)[0]
# Google chrome does not like cookies set to .localhost, so
# we just go with no domain then. Flask documents anyways that
# cross domain cookies need a fully qualified domain name
if rv == '.localhost':
rv = None
# If we infer the cookie domain from the server name we need
# to check if we are in a subpath. In that case we can't
# set a cross domain cookie.
if rv is not None:
path = self.get_cookie_path(app)
if path != '/':
rv = rv.lstrip('.')
return rv
Run Code Online (Sandbox Code Playgroud)
和get_cookie_domain(
你做同样的事情会看到:
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
...
Run Code Online (Sandbox Code Playgroud)
好.现在我们只需要找出要使用的域名.挖掘文档或代码,您将看到save_session()
在请求上下文中调用.所以你只需request
要从flask
模块中导入对象:
from flask import request
Run Code Online (Sandbox Code Playgroud)
并在里面使用它save_session()
来确定cookie的域名(例如从Host
头部),如下所示:
def save_session(self, app, session, response):
domain = '.' + request.headers['Host']
path = self.get_cookie_path(app)
# the rest of the method is intact
Run Code Online (Sandbox Code Playgroud)
您需要指定cookie域的唯一时间是使用响应对象发回它们.
还要记住,Host
标题可能不存在.
要整理整个事情,您需要指定您的版本(子类)SecureCookieSessionInterface
:
app = Flask(__name__)
app.session_interface = MySessionInterface()
Run Code Online (Sandbox Code Playgroud)
更多文档链接: