前端:React 后端:Django CD:用于前端和后端的 Heroku SSL:True
如螢屏截圖所示,cookie 顯示在我的請求標頭中,但未顯示在我的 cookie 開發人員工具中。對于 CSRF Token,我使用了@ensure_csrf_token 并配置了螢屏截圖所示的設定,但它仍然沒有顯示在瀏覽器開發者工具中。我還從前端發送了帶有憑據的請求,并允許來自后端的憑據,這也附在螢屏截圖中。cookie 可以從掛鎖資訊中看到,但不能從開發者工具中看到。

當團隊使用 monorepo 時,前端和后端都部署到不同的 heroku 容器。

cookie 如何從 Django 發送到 React 的示例
response = HttpResponse() response.set_cookie(key='refreshToken', value = refreshToken, max_age = 12 * 60 * 60, samesite="None", secure=True)
一切都在本地運行良好,直到我使用 Heroku CD 之后它就變酸了。未將 Cookie 設定為瀏覽器使我無法在使用 js-cookie 庫時獲取 cookie。
我已確保 cookie 是安全的,并且將 samesite 設定為 none,因為前端和后端都部署到具有不同 heroku URL 的不同容器中。到目前為止,其他執行緒提供的解決方案也沒有任何效果。
uj5u.com熱心網友回復:
對于同樣面臨此問題的任何人,請確保前端和后端都指向同一個域。Cookie 不會通過跨域設定到瀏覽器。
在 HttpResponse 中也包含域
response = HttpResponse() response.set_cookie(key='refreshToken', value = refreshToken, max_age = 12 * 60 * 60, samesite="None", domain=".yourdomain.com", secure=True)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/531126.html
標籤:反应herokudjango-rest-framework设置cookiedjango-csrf
