簡體   English   中英

如何在 CloudFront 分配后面的 static S3 站點上訪問 cookies?

[英]How can you access cookies on a static S3 site behind a CloudFront distribution?

所以我正在嘗試部署在本地環境中運行良好的網站,但是當它部署到 Cloudfront 時,它似乎無法訪問 cookies。

我的前端技術棧如下:托管在 S3 上的 Angular 站點,前面的雲端分發,具有有效 ssl 證書的自定義域名。

當用戶導航到登錄頁面時,他們可以成功提交論壇,並且服務器響應Set-Cookie header 中的 JWT 令牌。

設置 Cookie 標頭

但在此之后,在 angular 站點中,它說access-token cookie 不存在。 這里奇怪的是,在后續請求中, access-token cookie 實際上被轉發回后端。 (在下圖中,再次按下登錄按鈕,因此響應 cookie 與請求 cookie 相同。)

通過網絡接收和發送令牌的屏幕截圖。

我確保未設置HttpOnly ,並且前端和后端都托管在同一個根域frontend.root.comapi.root.com

Cloudfront 已配置為轉發訪問令牌 cookie:

緩存策略 緩存鍵設置:標題 - 無,cookie - 無,查詢字符串 - 無

源請求策略(請注意,當我將此設置為轉發所有 cookies 而不僅僅是訪問令牌時,它仍然不起作用): 源請求設置:標頭 - 所有查看器標頭,cookie - 訪問令牌,查詢字符串 - 全部

響應頭設置 響應標頭的 CORS 設置

因此,在我的 angular 站點中,在/login api 調用解析后,我使用ngx-cookie-service檢查並嘗試檢索 cookie。

this.cookieService.check('access-token'); // checks if it exists, returns false
this.cookieService.get('access-token'); // returns '' meaning the cookie does not exist

有關如何解決此問題並從我的 angular 站點中訪問 cookies 的任何想法? 如果需要,我可以提供有關我的配置的更多信息。 謝謝!

As you can barely make out in the screenshot the Cookies have the domain set as something starting with a suggesting that it is api.root.com , most importantly it is not frontend.root.com and not root.com .

服務器需要將 cookie 的域設置為root.com以便它可用於它的所有子域。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM