简体   繁体   English

如何在Tomcat中为JSESSIONID和jvmRoute更改定界符/分隔符?

[英]How to change the delimiter/separator in Tomcat for JSESSIONID and jvmRoute?

Using Tomcat 9.0.13, openJDK 8 running on rhel 7. 使用Tomcat 9.0.13,在rhel 7上运行的openJDK 8。
Our application used to run on Websphere, where the session-route separator can be specified with the CloneSeparatorChange. 我们的应用程序过去曾在Websphere上运行,其中可以使用CloneSeparatorChange指定会话路由分隔符。 The JSESSIONID is stored in a cookie set to Secure and HttpOnly. JSESSIONID存储在设置为Secure和HttpOnly的cookie中。

Tomcat normally uses "." Tomcat通常使用“。” as its separator between the JSESSIONID and the jvmRoute (CloneID in the Websphere world), but our code is expecting the ":" set in Websphere. 作为JSESSIONID和jvmRoute(在Websphere世界中为CloneID)之间的分隔符,但是我们的代码期望在Websphere中设置“:”。

My question: Is there a way to change the sessionid separator from "." 我的问题:有没有办法将会话ID分隔符从“。”更改为 to another character, say ":" ? 换一个字符,说“:”?

jvmRoute is handled by implementation of the SessionIdGenerator Component interface . jvmRoute由SessionIdGenerator Component接口的实现处理。 The Standard implementation org.apache.catalina.util.StandardSessionIdGenerator doesn't offer a way to configure the separator used to add jvmRoute to JSESSIONID : https://github.com/apache/tomcat/blob/trunk/java/org/apache/catalina/util/StandardSessionIdGenerator.java#L52-L59 : 标准实现org.apache.catalina.util.StandardSessionIdGenerator没有提供配置用于向JSESSIONID添加jvmRoute的分隔符的方法: https : //github.com/apache/tomcat/blob/trunk/java/org/apache /catalina/util/StandardSessionIdGenerator.java#L52-L59

if (route != null && route.length() > 0) {
    buffer.append('.').append(route);
} else {
    String jvmRoute = getJvmRoute();
    if (jvmRoute != null && jvmRoute.length() > 0) {
        buffer.append('.').append(jvmRoute);
    }
}

'.' '' character is hard coded so you can't change it by configuration. 字符是硬编码的,因此您不能通过配置对其进行更改。 Fortunately, you can configure the class used to generate the SessionId: 幸运的是,您可以配置用于生成SessionId的类:

My suggestion is to extend the StandardSessionIdGenerator class, override the generateSessionId by calling the super method and replacing the character. 我的建议是扩展StandardSessionIdGenerator类,通过调用super方法并替换字符来覆盖generateSessionId。

class CustomSessionIdGenerator extends StandardSessionIdGenerator{
    @Override
    public String generateSessionId(String route) {
        String sessionId = super(route);
        return sessionId.replace('.',':');
    }
}

You can then configure your CustomSessionIdGenerator class in a Manager component inside a Context component in tomcat's configuration files like context.xml . 然后,您可以在tomcat的配置文件(例如context.xml)中的Context组件内的Manager组件中配置CustomSessionIdGenerator类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM