如何在tomcat 8中将Cookie处理器更改为LegacyCookieProcessor

Sac*_*rma 18 java cookies tomcat8

我的代码正在处理tomcat 8版本8.0.33但是在8.5.4我得到:为此cookie指定了无效的域[.mydomain].

我发现Rfc6265CookieProcessor是在tomcat 8最新版本中引入的.

它在官方文档上说这可以在context.xml中恢复为LegacyCookieProcessor,但我不知道如何.

请让我知道如何做到这一点.

谢谢

小智 22

您可以在context.xml中尝试

<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
Run Code Online (Sandbox Code Playgroud)

参考:https: //tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html


Iam*_*yAV 13

案例1:您正在使用独立的Tomcat访问,以改变文件的Tomcat服务器

请按照@linzkl 的回答

案例 2:您使用的是Standalone Tomcat,但您无权更改 tomcat 服务器中的文件

在您的应用程序的src/main/webapp/META-INF文件夹下创建一个名为 context.xml 的新文件并粘贴下面给出的内容

<?xml version="1.0" encoding="UTF-8"?> 
<Context>
  <WatchedResource>WEB-INF/web.xml</WatchedResource>
  <WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
  <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> 
  <CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
</Context>
Run Code Online (Sandbox Code Playgroud)

当您在独立 Tomcat 中部署应用程序时,您放置在 META-INF 文件夹下的 context.xml 文件将覆盖在 tomcat/conf/context.xml 中给出的 context.xml 文件

注意:如果您遵循此解决方案,则必须为每个应用程序执行此操作,因为META-INF/context.xml是特定于应用程序的

案例 3:您使用的是嵌入式 Tomcat

为 WebServerFactoryCustomizer 创建一个新的 bean

@Bean
WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
    return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {

        @Override
        void customize(TomcatServletWebServerFactory tomcatServletWebServerFactory) {
            tomcatServletWebServerFactory.addContextCustomizers(new TomcatContextCustomizer() {
                @Override
                public void customize(Context context) {
                    context.setCookieProcessor(new LegacyCookieProcessor());
                }
            });
        }
    };
}
Run Code Online (Sandbox Code Playgroud)


小智 10

启用在以前版本的Tomcat中使用的LegacyCookieProcessor已经解决了我的应用程序中的问题.正如linzkl所提到的,在Apache的网站https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html中对此进行了解释.

原因是新版本的Tomcat不明白.(点)在正在使用的Cookie的域名前面.

此外,请确保在使用Internet Explorer时检查此帖子.显然,它很可能会破裂.

您可以在以下路径中找到context.xml.

tomcat8/CONF/context.xml中

<?xml version="1.0" encoding="UTF-8”?>
<!-- The contents of this file will be loaded for each web application —>
<Context>
<!-- Default set of monitored resources. If one of these changes, the    -->
<!-- web application will be reloaded.                                   -->

<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- <Manager pathname="" /> -->
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor"/>
</Context>
Run Code Online (Sandbox Code Playgroud)