使用会话时,Flask需要一个密钥.在我看过的每个例子中,秘密密钥以某种方式生成,然后存储在源代码或配置文件中.
永久存储它的原因是什么?为什么不在应用程序启动时简单地生成它?
app.secret_key = os.urandom(50)
Run Code Online (Sandbox Code Playgroud) 我有一个用Flask编写的应用程序,并尝试使用Flask-Dance(Flask-Dance Docs - Google示例)来启用Google OAuth.我得到了以下设置:
from flask import redirect, url_for, jsonify, Blueprint
from flask_dance.contrib.google import make_google_blueprint, google
from server.app import app
# Internal auth blueprint
auth = Blueprint('auth', __name__, url_prefix='/auth')
# Google auth blueprint
google_login = make_google_blueprint(
client_id=app.config['GOOGLE_CLIENT_ID'],
client_secret=app.config['GOOGLE_CLIENT_SECRET'],
scope=['profile', 'email']
)
def auth_google_view():
"""
Authenticate user with google
"""
# Not authorized
print(google.authorized)
if not google.authorized:
return redirect(url_for('google.login'))
# Authorized - check data
user_info = google.get('/oauth2/v2/userinfo')
if user_info.ok:
return jsonify({'status': 'ok', 'email': user_info.json() .['email']}), 200
return jsonify({'status': …Run Code Online (Sandbox Code Playgroud) 我正在使用Gunicorn作为Web服务器运行Flask应用程序.整个项目部署到Heroku.
Procfile
web: gunicorn app:app --log-file=-
Run Code Online (Sandbox Code Playgroud)
Flask会话在服务器端实现,只有会话ID存储在flask.session对象中.每当我尝试登录时,我都会首先正确登录,然后重定向到起始站点(应该是用户站点).
LoginController.py
def login(form) :
User.session.set(User.getByLogin(form))
if User.session.exists() :
return redirect(Urls.home)
return redirect(Urls.login)
Run Code Online (Sandbox Code Playgroud)
日志显示User.session.exists()返回True但在下一个方法中(在重定向期间)...
HomeController.py
def view() :
if User.session.exists() :
return CourseController.view()
return render_template("home.html")
Run Code Online (Sandbox Code Playgroud)
...同样的方法返回False.
User.session对象
def exists(self) :
key = session.get("user_key")
user = self.users.get(key)
Log.debug("session::exists", user = user)
return user is not None
Run Code Online (Sandbox Code Playgroud)
在以下所有请求中,用户是否随机登录.
这可能是什么原因?我听说过大的session对象会导致数据丢失,但我只会在其中存储整数.
这让我非常疯狂,并阻止我进行本地开发/测试。
我有一个使用 authlib(仅限客户端功能)的烧瓶应用程序。当用户点击我的主页时,我的 Flask 后端会将他们重定向到 /login,然后又重定向到 Google Auth。Google Auth 然后将它们发回我的应用程序的 /auth 端点。
几个月来,我一直在遇到 authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF 警告的临时问题!状态在请求和响应中不相等。感觉就像一个 cookie 问题,大多数时候,我只是打开一个新的浏览器窗口或隐身或尝试清除缓存,最终,它有点工作。
但是,我现在在 docker 容器内运行完全相同的应用程序,并且在一个阶段这是有效的。我不知道我做了什么改变,但是每当我浏览到 localhost/ 或 127.0.0.1/ 并通过身份验证过程(每次清除 cookie 以确保我没有自动登录)时,我经常被重定向回 localhost /auth?state=blah blah blah 我遇到了这个问题:authlib.integrations.base_client.errors.MismatchingStateError: mismatching_state: CSRF 警告!状态在请求和响应中不相等。
我认为我的代码的相关部分是:
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def catch_all(path: str) -> Union[flask.Response, werkzeug.Response]:
if flask.session.get("user"):
return app.send_static_file("index.html")
return flask.redirect("/login")
@app.route("/auth")
def auth() -> Union[Tuple[str, int], werkzeug.Response]:
token = oauth.google.authorize_access_token()
user = oauth.google.parse_id_token(token)
flask.session["user"] = user
return flask.redirect("/")
@app.route("/login")
def login() -> werkzeug.Response:
return oauth.google.authorize_redirect(flask.url_for("auth", _external=True))
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何帮助。 …