【发布时间】:2022-01-07 22:07:14
【问题描述】:
我有一个前端 vue 站点托管在 google 的 firebase 上,网址为 (https://front-end.web.com),而我的烧瓶后端托管在 heroku 上,网址为 (https://back-end.heroku.com)。这使我的会话不会在请求之间持续存在,我尝试通过在后端实现 CORS 来解决此问题,但由于某种原因它不起作用,下面是我的代码的 sn-ps 以显示我的实现
config_class.py
class ConfigClass():
CORS_ALLOW_HEADERS = ['Content-Type']
CORS_ORIGINS = ['https://front-end.web.com']
SECRET_KEY = os.environ.get("APP_SECRET_KEY")
SESSION_TYPE = 'redis'
_初始化.py
from flask import Flask, session
from flask_session import Session
from flask_cors import CORS
from root_folder.config import ConfigClass
db = SQLAlchemy()
migrate = Migrate()
ma = Marshmallow()
sess = Session()
def create_app(ConfigClass):
# initiate the flask app and assign the configurations #
app = Flask(__name__)
app.config.from_object(config_options[config_class])
sess.init_app(app)
from root_folder.clients import clients_app
# register all the blueprints in this application
app.register_blueprint(clients_app)
CORS(app, supports_credentials=True)
# return the app object to be executed
return app
app.py
from root_folder import create_app
app = create_app()
过程文件:
web: gunicorn -w 1 app:app
axios 前端请求
let formData = new FormData();
formData.append("email", email);
formData.append("password", password);
axios.post(
backendUrl+'create_client_account',
formData,
{
withCredentials: true,
headers:{
"Content-Type": "multipart/form-data"
}
}
);
创建客户端路由(我已将此代码块剥离到最低限度以使其易于理解):
from flask import session
# route for creating account credentials
@bp_auth_clients_app.route("/create_client", methods=["POST"])
def create_client():
username = request.form.get("username").lower()
email = request.form.get("email").lower()
# create account code goes here #
auth_authentication = True
session["auth_authentication"] = auth_authentication
req_feedback = {
"status": True,
"message": "Account was successfully created",
"data": feedback_data
}
return jsonify(req_feedback), 200
账号创建成功后,后续请求无法访问session值,返回None。
为了在本地服务器上重现问题,我通过域 "localhost:8080" 访问前端,同时通过 "127.0.0.1:8000" 访问烧瓶服务器。如果我把前端域改成“127.0.0.1:8080”,一般是没有问题的。
请给点建议。
【问题讨论】:
标签: flask session axios cross-domain