Java 更改 cookie JSESSIONID 名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/877064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:32:27  来源:igfitidea点击:

Changing cookie JSESSIONID name

javatomcatjsessionid

提问by ramanr

I have a requirement of having to run multiple tomcat server in single physical box. While accessing these from a browser, when user switches between the applications, it results in logging out the user previously access application. This is because of JSESSIONID cookie conflict.

我需要在单个物理盒中运行多个 tomcat 服务器。从浏览器访问这些时,当用户在应用程序之间切换时,会导致用户退出以前访问的应用程序。这是因为 JSESSIONID cookie 冲突。

One possible solution is to run each applications in different context. Unfortunately, my applications will not work in context path setting as none of the resources are accessed with request.getContextPath() prepended in front.

一种可能的解决方案是在不同的上下文中运行每个应用程序。不幸的是,我的应用程序将无法在上下文路径设置中工作,因为没有任何资源是通过前置的 request.getContextPath() 访问的。

This leaves me to change the name of cookie JSESSIONID to resolve the conflict. Is there a way to do this? If yes, how?

这让我不得不更改 cookie JSESSIONID 的名称来解决冲突。有没有办法做到这一点?如果是,如何?

Hope I'm clear in stating my question.

希望我清楚地陈述我的问题。

Note: All my application are running in different port in the same machine.

注意:我所有的应用程序都在同一台机器的不同端口上运行。

回答by Martin

I don't think it's possible at this point - see https://issues.apache.org/bugzilla/show_bug.cgi?id=42419

我认为此时不可能 - 请参阅https://issues.apache.org/bugzilla/show_bug.cgi?id=42419

The last entry states "This has been fixed in 5.5.x and will be included in 5.5.28 onwards" - which is the next point release - 5.5.27 is the current release.

最后一个条目指出“这已在 5.5.x 中修复,并将包含在 5.5.28 之后” - 这是下一个版本 - 5.5.27 是当前版本。

回答by matt b

Not 100% sure if this will work, but you can use the jvmRouteattribute, which is generally used in a load-balanced/clustered environment for the load balancers to be able to tell the nodes apart. Example:

不能 100% 确定这是否有效,但您可以使用该jvmRoute属性,该属性通常用于负载平衡/集群环境中,以便负载平衡器能够区分节点。例子:

<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">

This will end up generating a JSESSIONID value that looks like "ABCDEF123456.node1".

这最终会生成一个类似于“ABCDEF123456.node1”的 JSESSIONID 值。

Documentation link.

文档链接

回答by user489641

By Using two following system properties this can be achieved with ease.

通过使用以下两个系统属性,可以轻松实现这一点。

  • org.apache.catalina.SESSION_COOKIE_NAME
  • org.apache.catalina.SESSION_PARAMETER_NAME
  • org.apache.catalina.SESSION_COOKIE_NAME
  • org.apache.catalina.SESSION_PARAMETER_NAME

Any value can be passed to above properties to change the default values.

任何值都可以传递给上述属性以更改默认值。

Here complete detailswith some sample script is found.

在这里可以找到一些示例脚本的完整详细信息

回答by timkingman

Tomcat 7 moves this from org.apache.catalina.SESSION_COOKIE_NAME to an attribute on the main <Context> config. http://tomcat.apache.org/migration-7.html#Session_manager_configuration

Tomcat 7 将其从 org.apache.catalina.SESSION_COOKIE_NAME 移动到主 <Context> 配置上的一个属性。 http://tomcat.apache.org/migration-7.html#Session_manager_configuration

回答by Joseph Lust

The following works for me on Tomcat7 in the context.xml file:

以下内容在 Tomcat7 上的 context.xml 文件中对我有用:

<Context path="/yourApp" sessionCookieName="custom_session_id">

回答by walv

Everything is much simpler with Servlet API 3.0.

使用 Servlet API 3.0,一切都变得简单多了。

Now you can configure it in your web.xml:

现在你可以在你的 web.xml 中配置它:

<session-config>
    <cookie-config>
        <name>MY_JSESSIONID_YAHOOOOOO</name>
    </cookie-config>
</session-config>

That's it!

就是这样!