如何通过管理器路径名在 Spring Boot 中禁用 Tomcat 会话持久性? [英] How to disable Tomcat session persistence in Spring Boot via Manager pathname?

查看:26
本文介绍了如何通过管理器路径名在 Spring Boot 中禁用 Tomcat 会话持久性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Tomcat 中,conf/context.xml 中有一个众所周知的配置选项来禁用会话持久性:

In Tomcat there is a well known configuration option in conf/context.xml to disable session persistence:

<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<Manager pathname="" />

当如此处所示取消注释时,org.apache.catalina.Manager 的应用实现(例如 org.apache.catalina.session.StandardManager)没有pathname 告诉它在哪里存储会话到磁盘,因此它不会将会话文件写入磁盘(例如在关机时),这正是我们想要的.

When uncommented as shown here, the applied implementation of org.apache.catalina.Manager (e.g. org.apache.catalina.session.StandardManager) does not have a pathname to tell it where to store sessions to the disk, and thus it does not write session files to disk (e.g. on shutdown), which is what we want.

换句话说,这会禁用标准 Tomcat 功能以通过服务器重启来维持会话.

In other words, this disables the standard Tomcat feature to sustain sessions through server restart.

如何在带有嵌入式 Tomcat 的 Spring Boot 中实现相同的功能?

How can the same be achieved in Spring Boot with embedded Tomcat?

也许可以通过某种方式获取 Manager 对象以将属性路径名设置为 null?

Perhaps the Manager object can somehow be obtained to set the property pathname to null?

推荐答案

您可以使用 TomcatContextCustomizer 来访问管理器并应用必要的配置:

You can use a TomcatContextCustomizer to access the manager and apply the necessary configuration:

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    tomcat.addContextCustomizers(new TomcatContextCustomizer() {

        @Override
        public void customize(Context context) {
            if (context.getManager() instanceof StandardManager) {
                ((StandardManager) context.getManager()).setPathname("");
            }
        }
    });
    return tomcat;
}

这篇关于如何通过管理器路径名在 Spring Boot 中禁用 Tomcat 会话持久性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆