greppen SESSION_COOKIE_DOMAIN
durch Flask des Github Repo man sehen kann, dass es wie this verwendet wird:
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
das gleiche tun mit get_cookie_domain(
du wirst see:
def save_session(self, app, session, response):
domain = self.get_cookie_domain(app)
path = self.get_cookie_path(app)
...
OK. Jetzt müssen wir nur herausfinden, welcher Domainname verwendet werden soll. Wenn Sie docs oder code durchsuchen, werden Sie sehen, dass save_session()
im Kontext der Anfrage aufgerufen wird. So müssen Sie nur das request
Objekt aus flask
Modul importieren:
from flask import request
und verwenden Sie es innerhalb save_session()
Domain-Namen für die Cookies, um zu bestimmen (zB aus dem Host
Header) wie folgt aus:
def save_session(self, app, session, response):
domain = '.' + request.headers['Host']
path = self.get_cookie_path(app)
# the rest of the method is intact
Die Die Zeit, die Sie brauchen, um Cookies zu definieren, ist, wenn Sie sie mit dem Antwortobjekt zurücksenden.
Beachten Sie auch, dass Host
Kopfzeile abwesend sein kann.
verdrahten die ganze Sache Sie Ihre Version (Unterklasse) von SecureCookieSessionInterface
benötigen angeben:
app = Flask(__name__)
app.session_interface = MySessionInterface()
Mehr doc Links:
Hallo Twil, ich verstehe das meiste von dem, was du gesagt hast, außer dem letzten p Kunst. Ich kann den Domain-Namen bekommen, indem ich: domain = request.headers ['Host'] Allerdings verstehe ich nicht, was Sie mit "und verwenden Sie es innerhalb save_session(), um Domain-Namen für die Cookies zu bestimmen" . Meine Frage ist: Sobald ich den Domain-Namen bekomme, wo/wie stelle ich ihn ein? Vielen Dank. –
Bitte, siehe aktualisierte Antwort. Hoffe, du hast die Idee. – twil
Hallo, ich bin neu in der Flasche/Python, aber ich habe die Idee. Ich werde versuchen, es später zu verkabeln, danke. –