我有一個前端 vue 站點托管在 google 的 firebase 上,網址為(https://front-end.web.com),而我的燒瓶后端托管在 heroku 上,網址為(https://back-end.heroku.com)。這使我的會話不會跨請求持續存在,我嘗試通過在后端實作 CORS 來解決此問題,但由于某種原因它不起作用,下面是我的代碼片段以顯示我的實作
配置類.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
應用程式.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”,我通常不會有任何問題。
請就該怎么做提出建議。
uj5u.com熱心網友回復:
會話使用 cookie:在創建會話時,服務器將在 set-cookie 標頭中發送 cookie 值。由于跨源問題,它對您不起作用。
當你使用它時它對你很好,127.0.0.1因為127.0.0.1:8080和127.0.0.1:8000是相同的來源,所以瀏覽器接受set-cookie標題并設定 cookie 沒問題。
Cookie 在每個請求的標頭中發送,您的服務器Redis通過 cookie 值加載會話(cookie 值稱為 session_id)。
它是如何插入的 => 通常你的會話會在請求生命周期結束時被序列化并插入到 Redis 中,并將 cookie 哈希作為 Key。
如果您想繼續使用會話和 cookie,您需要為您的部署找到另一個解決方案,以使您的后端和前端具有相同的主機名。
如果您不能這樣做,我建議您閱讀有關JWT (Json-Web-Tokens) 的資訊。
編輯 您可以在回應正文中發送會話 ID 并將其保存在本地存盤中。
然后你需要配置:
frontend在Authorizationheader base64編碼中設定session id值。
后端base64 解碼Authorization請求中的標頭值并檢查 Redis 中的會話,如果存在則加載它。
編輯
如何使用 apache 在同一主機名上部署后端/前端:使用 apache,您需要創建 2 個虛擬主機,一個用于后端,另一個用于在不同埠上偵聽的前端,然后配置您的 Web 服務器部署以使用后端 VH(如果路徑是前綴)通過/api/并將前端虛擬主機用于其他任何事情。這樣,您對您的 api 提出的任何請求,您的后端都會處理它,否則它將為您的前端應用程式提供服務。這只是如何做到這一點的一種方法還有很多其他
檢查這個問題。
uj5u.com熱心網友回復:
感謝 Ahmad 的建議,我能夠為我的前端和后端使用自定義域來解決問題,如下所示:
frontend.herokuapp.com -> customDomain.com
backend.herokuapp.com -> api.customDOmain.com
最后我將下面的行添加到我的會話配置中:
SESSION_COOKIE_DOMAIN = ".customDomain.com"
一切都很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406534.html
標籤:
上一篇:無法獲取火花案例類的輸出
