java - 在 java-spring-tomcat 上设置 session cookie 属性 "Domain"

标签 java spring spring-mvc tomcat cookies

如何在 tomcat 服务器上为我的 spring 项目设置 session cookie 属性“Domain”?我正在使用 spring security 和 HTTPS 目前我有以下内容:

Set-Cookie: JSESSIONCookie: JSESSIONID=DEAC4422AB4E28A7062C08724C8BCFAA; Path=/myapp/; Secure; HttpOnly.

但是我想要这个

Set-Cookie: JSESSIONCookie: JSESSIONID=DEAC4422AB4E28A7062C08724C8BCFAA; Path=/myapp/; Secure; Domain=.localhost; HttpOnly.

我尝试使用 cookie-config 将域属性放入 web.xml 中。 我试过使用 spring session [CookieHttpSessionStrategy],但这不起作用。

有类似 TomcatContextCustomizer 的东西,但它不起作用 [实际上我可能在配置它时犯了一些错误]

此外,如果在本地主机上运行,​​我应该设置什么域?

obj.setDomain("localhost");//or
obj.setDomain(".localhost");//or
obj.setDomain("127.0.0.1");

以上哪一项是正确的?

最佳答案

您可以使用 tomcat 配置属性:sessionCookieDomain

The domain to be used for all session cookies created for this context. If set, this overrides any domain set by the web application. If not set, the value specified by the web application, if any, will be used.

<context  sessionCookiePath="/myapp/" sessionCookieDomain=".localhost">
...
</context>

在全局配置 config/context.xml 或特定于应用程序的上下文文件中。

@见Tomcat configuration documentation

关于java - 在 java-spring-tomcat 上设置 session cookie 属性 "Domain",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34808694/

相关文章:

java - 关于 JPanels 和菜单的最佳选择

java - Guice:如何根据(动态网络属性)在运行时更改注入(inject)

java - 数组未正确存储值

java - 是否可以停用 Spring Integration 对现有消息 channel 的检查?

spring - @Value 注解和 Environment API 的区别?

java - 使用 Spring MVC 发送 html 邮件的 NULL 指针异常

java - 以 HashMap<string,integer> 作为其类型对 TreeSet 进行排序

javascript - AngularJS:添加依赖项(内联数组与注入(inject))

java - 如何使用LDAP登录网站

spring-mvc - 在tiles和spring mvc中加载资源