Spring 启用转发后启动 cookie 域问题 headers
Spring Boot cookie domain issue after enabling forward headers
在我的 Spring 引导应用程序(带有嵌入式 Tomcat)中,我通过将 server.use-forward-headers
属性 设置为 [=13] 来启用转发 headers =] 在我的 application.properties
.
由于 X-Forwarded-Host
在执行此操作时出现问题,讨论了 here, I added the suggested code snippet 以使其正常工作。一切正常,但我注意到我的 cookie 上的域已更改。
具体来说,如果在子域上,我的 JSESSIONID
域曾经是 example.com
或 abc.example.com
。添加该代码段后,我的 JESSIONID
域始终是 .example.com
.
我不太清楚为什么添加该代码会更改 cookie。有什么想法吗?
事实证明,我的 JSESSIONID
cookie 有一个自定义的 Cookie 序列化程序,但它无法正常工作。对于我的自定义 Cookie 序列化程序,我从 https://docs.spring.io/spring-session/docs/current/reference/html5/guides/custom-cookie.html.
复制了代码片段
在启用转发 headers 之前 DomainNamePattern
不匹配,因此 cookie 域是 example.com
。启用转发 headers(特别是 X-Forwarded-Host
)后,域现在匹配,导致自定义序列化程序将 cookie 域设为 .example.com
.
为了避免对我的用户造成潜在的奇怪行为,或者不得不使所有用户 JSESSIONID
cookie 无效,我选择注释掉域名模式匹配器,并坚持使用 example.com
作为现在的 cookie 域。
在我的 Spring 引导应用程序(带有嵌入式 Tomcat)中,我通过将 server.use-forward-headers
属性 设置为 [=13] 来启用转发 headers =] 在我的 application.properties
.
由于 X-Forwarded-Host
在执行此操作时出现问题,讨论了 here, I added the suggested code snippet 以使其正常工作。一切正常,但我注意到我的 cookie 上的域已更改。
具体来说,如果在子域上,我的 JSESSIONID
域曾经是 example.com
或 abc.example.com
。添加该代码段后,我的 JESSIONID
域始终是 .example.com
.
我不太清楚为什么添加该代码会更改 cookie。有什么想法吗?
事实证明,我的 JSESSIONID
cookie 有一个自定义的 Cookie 序列化程序,但它无法正常工作。对于我的自定义 Cookie 序列化程序,我从 https://docs.spring.io/spring-session/docs/current/reference/html5/guides/custom-cookie.html.
在启用转发 headers 之前 DomainNamePattern
不匹配,因此 cookie 域是 example.com
。启用转发 headers(特别是 X-Forwarded-Host
)后,域现在匹配,导致自定义序列化程序将 cookie 域设为 .example.com
.
为了避免对我的用户造成潜在的奇怪行为,或者不得不使所有用户 JSESSIONID
cookie 无效,我选择注释掉域名模式匹配器,并坚持使用 example.com
作为现在的 cookie 域。