我的代码在 Tomcat 8 版本 8.0.33 上运行正常,但在 8.5.4 上出现以下错误:An invalid domain [.mydomain] was specified for this cookie.
我发现在 Tomcat 8 的最新版本中引入了 Rfc6265CookieProcessor。
官方文档上说可以在 context.xml 中将其还原为 LegacyCookieProcessor,但我不知道如何操作。
请告诉我如何进行操作,谢谢。
我的代码在 Tomcat 8 版本 8.0.33 上运行正常,但在 8.5.4 上出现以下错误:An invalid domain [.mydomain] was specified for this cookie.
我发现在 Tomcat 8 的最新版本中引入了 Rfc6265CookieProcessor。
官方文档上说可以在 context.xml 中将其还原为 LegacyCookieProcessor,但我不知道如何操作。
请告诉我如何进行操作,谢谢。
你可以在context.xml中尝试。
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
参考: https://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html
情况1:您正在使用独立的Tomcat,并且可以访问以更改Tomcat服务器中的文件
请遵循@linzkl的答案
情况2:您正在使用独立的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>
当您在独立的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());
}
});
}
};
}
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>
Tomcat版本<8.5.47的SameSite问题已解决
在Tomcat 8.5.47及以下版本(Tomcat 8版本)中,由于Tomcat中的一个错误,将CookieProcessor
标签设置为启用同站点(如下所示)在context.xml
中无效。
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" sameSiteCookies="none" />
CookieProcessor
类来解决问题。CookieProcessor
实现和部署步骤的详细信息。LegacyCookieProcessor
源代码编写了一个自定义的CookieProcessor
类,使得tomcat 8.5.47能够启用cookie中的SameSite
属性。