我正在登錄一個應用程式以與 Flask 中的后端做出反應,但會話無法正常作業。
Flask 應用程式在 pythonanywhere 上的遠程服務器中運行,我正在 localhost:3000 上測驗 React 應用程式
這是燒瓶應用程式的代碼:
app = Flask(__name__)
app.config["SESSION_PERMANENT"] = True
app.config["SESSION_TYPE"] = "filesystem"
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=5)
Session(app)
CORS(app, supports_credentials = True, resources={r"/webapp/*": {"origins": "http://localhost:3000/"}})
@app.route("/webapp/login", methods = ["POST"])
def login():
user = request.get_json(force=True)
Email = user.get("email", "")
Password = user.get("password", "")
login = Login.login(Email, Password)
if login["Success"]:
session["email"] = Email
return login
@app.route("/webapp/checksession", methods = ["GET"])
def checksession():
sessionEmail = session.get("email", "")
if sessionEmail == "":
return utils.returnResult(False, "Session not valid")
return utils.returnResult(True, "")
在 React 應用程式中,我使用 axios 進行登錄并檢查服務器中的會話,例如:
axios.get(APIURL CHECK_SESSION, {withCredentials: true})
.then(response => {
console.log(response.headers);
setSession(response.data.success)
setCheckedSession(true)
})
.catch(err => {
setSession(false)
setCheckedSession(true)
if(err.response) {
}
else {
window.alert('Could not establish a connection to the server!');
}
});
在開發工具中,我們可以看到服務器正在發送 cookie 會話:

但是當我檢查應用程式的 cookie 時,那里什么也沒有:
(對不起葡萄牙人)
在 axios promise 處理程式中,當我列印回應的標頭時,這就是我得到的全部內容:

所以每次反應應用程式檢查用戶是否在服務器中有一個有效的會話,服務器創建一個新的會話,這意味著反應應用程式沒有保存和發送 cookie 到服務器。
另外,當我用郵遞員測驗它時,一切正常。
我搜索了所有地方,但找不到答案。誰能幫我弄清楚我做錯了什么?
uj5u.com熱心網友回復:
原來它在燒瓶會話中缺少配置:
app.config["SESSION_COOKIE_SAMESITE"] = "None"
app.config["SESSION_COOKIE_SECURE"] = True
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/534745.html
標籤:反应烧瓶会议饼干公理
上一篇:Fatalerror:UncaughtError:Calltoundefinedfunctionheader()-初學者問題
