这让我非常疯狂,并阻止我进行本地开发/测试。
我有一个使用 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)
我将不胜感激任何帮助。 …