在测试一个程序的时候,出现了“The HTTP header line [connection : Keep-alive] does not conform to RFC 7230 and has been ignored”这个错误;经搜索关键字 RFC 7230发现是tomcat高版本中,严格按照规范解析。
百度了一下解决方案, 都是千篇一律,或许最初的作者是解决了问题的,但是对于我这一版本(spring-boot-starter-tomcat:2.3.3.Release,即tomcat版本为9.0.37)无效,常见搜索的springBoot2.x的解决方案如下:
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "|{}[]"));return factory;
}原文链接如下:
https://www.freesion.com/article/2397892175/
最终解决方案如下:
package net.dlet.dhdemo.configure;import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** @Description: 配置转义字符,解决当请求路径中特殊字符,解决高版本Tomcat对接口参数限制字符,解析失败从而出现RFC7230、RFC3986问题* @PackageName: net.dlet.dhdemo.configure* @Name: RF7230* @Author: cure* @CreateDate: 2020/09/06 00:30* @ModifyUser:* @ModifyDate:* @ModifyDesc: 修改内容* @DayNameFull: 星期日* @ProjectName: dhdemo* @Version: 1.0**/
@Configuration
public class RFC7230 {/*** 解决Tomcat RFC7230问题* @return*/@Beanpublic ConfigurableServletWebServerFactory webServerFactory() {TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();factory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {connector.setProperty("relaxedQueryChars", "|{}[](),/:;<=>?@[\\]{}\\");connector.setProperty("relaxedPathChars", "|{}[](),/:;<=>?@[\\]{}\\");connector.setProperty("rejectIllegalHeader", "false");});return factory;}
}