簡體   English   中英

在springboot中跨子域共享cookie

[英]Share a cookie across subdomains in springboot

我有一個 web 應用程序,它托管在https://example.com上。 I would like to share a session cookie between the main domain https://example.com and sub-domain https://www.example.com . 因此,如果用戶從一個域切換到另一個域,則不需要重新登錄。 我如何在 springboot 2.2.6 中實現這一點?

這是我嘗試過的:我去了 application.properties 並設置server.servlet.session.cookie.domain=.example.com現在,這沒有幫助。 我收到一個錯誤:

java.lang.IllegalArgumentException: An invalid domain [.example.com] was specified for this cookie
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateDomain(Rfc6265CookieProcessor.java:210) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:145) ~[tomcat-embed-core-9.0.33.jar:9.0.33]
    at org.apache.catalina.connector.Response.generateCookieString(Response.java:973) ~[tomcat-embed-core-9.0.33.jar:9.0.33]

If I set server.servlet.session.cookie.domain=example.com , then the cookie is not visible for http://www.example.com and if I set server.servlet.session.cookie.domain=www.example .com ,則 cookie 對於http://example.com不可見

我已閱讀有關 Rfc6265CookieProcessor 和 LegacyCookieProcessor 的討論,但我不知道解決此問題的正確方法。

Springboot 2.2.6 使用 tomcat 版本 9.0.*

那么,我該如何解決這個問題呢?

編輯:

我只在本地主機上嘗試上述更改,而不是在生產環境中。 Instead of accessing http://www.example.com , I was doing https://www.localhost and instead of accessing http://example.com , I was doing http://localhost

正確的值是:

server.servlet.session.cookie.domain=example.com

我正在嘗試的是我在 localhost 上進行更改,但它們對我不起作用。 我正在手動修改 chrome 控制台中的值,並希望看到在 https://localhost 上設置的 cookies 與域 localhost 在域 https:// 的另一個選項卡中可見域https :// 並沒有發生。

我在這里閱讀了答案: Share cookie between subdomain and domain and @Cesc 對此答案的評論是:

我不確定該放在哪里,所以我選擇了已接受答案的評論。 在我的本地主機上證明上述內容需要很長時間並且實驗失敗,直到我想到我應該在名稱中使用一個點來調用本地主機。 像“localhost.com”或類似的東西。 然后所有的“設置cookies”行為都開始遵循這個答案中寫的解釋。 希望這可能對某人有所幫助。

因此,我直接嘗試了對生產的更改,並且效果很好。 我仍然無法讓它在本地主機上工作。 我在 localhost 上訪問我的網站的方式是:

https://localhost 和https://www.localhost Based on @Cesc 's comment, I probably need to access the website on localhost as https://www.localhost.com or https://localhost.com and then it will work. 但是,我還沒有嘗試過。

與其在您的開發機器上使用“localhost”進行測試,不如嘗試使用您機器的完全限定主機名。 在針對我們的單點登錄平台測試身份驗證時,我遇到了類似的挑戰。

暫無
暫無

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

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