Flask应用Cookie与Session丢失问题求助(Traefik代理场景)
问题详情
使用Flask 2.2.2版本(所有v2版本均已测试),流量通过Traefik代理转发。当请求来自http://domain.localhost时,根路径/无法读取Cookie和Session,但通过/callback路径跳转后可正常读取。尽管Cookie已存储在/路径下,但几乎所有浏览器都存在该问题。
相关代码
from datetime import timedelta import logging from flask import Flask, request, redirect, url_for, session, make_response app = Flask(__name__) app.permanent_session_lifetime = timedelta(minutes=5) @app.route("/callback/") def callback(): cookie = request.cookies.get('token') print(f'Entrypoint {request.headers.get("Host")} {session} {cookie}') session.permanent = True resp = make_response(redirect(url_for("home"))) resp.set_cookie(key="host", value="host", domain=f'{request.headers.get("Host")}') resp.set_cookie(key="proto", value="proto", domain=f'{request.headers.get("Host")}') resp.set_cookie(key="token", value="TestCookie", domain=f'{request.headers.get("Host")}') if "token" not in session: session["token"] = "No" else: session["token"] = f'{session["token"]}1' session.modified = True print("Redirect to Entrypoint") return resp @app.route("/", methods=['POST','GET']) def home(): print(f'Entrypoint {request.headers.get("Host")} {session} WHY IS THIS EMPTY AFTER LEAVING PAGE') if request.headers.get("X-Forwarded-Host") is None: session.modified = True print("leaving page to http://domain.localhost") return redirect(f"http://domain.localhost") host = "" bol = False if request.headers.get("X-Forwarded-Host") is not None: host = str(request.headers.get("X-Forwarded-Host")) proto = str(request.headers.get("X-Forwarded-Proto")) bol = True print("Redirect to Callback") return redirect(url_for("callback")) if __name__ == '__main__': # logging filename = 'logfile.log' logging.basicConfig(handlers=[ logging.FileHandler('logfile.log'), logging.StreamHandler() ], encoding='utf-8', level=logging.DEBUG, format='%(name)s - %(levelname)s : %(asctime)s - %(message)s') logging.getLogger('werkzeug').setLevel(logging.ERROR) app.config['SESSION_REFRESH_EACH_REQUEST'] = False app.secret_key = "123" app.run(port=8888, host='0.0.0.0', debug=True)
运行输出
* Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. * Serving Flask app 'test' * Debug mode: on * Tip: There are .env or .flaskenv files present. Do "pip install python-dotenv" to use them. ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No'}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1'}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11'}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No111'}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No1111'}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost ***Entrypoint 192.168.0.101:8888 <SecureCookieSession {}> WHY IS THIS EMPTY AFTER LEAVING PAGE*** Redirect to Callback Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No11111'}> None Redirect to Entrypoint Entrypoint 192.168.0.101:8888 <SecureCookieSession {'_permanent': True, 'token': 'No111111'}> WHY IS THIS EMPTY AFTER LEAVING PAGE leaving page to http://domain.localhost
内容的提问来源于stack exchange,提问作者TechGuy2794
相关产品推荐
相关产品推荐

