一、问题现象
请求接口报400,参数接收错误。
二、翻译
The valid characters are defined in RFC 7230 and RFC 3986的意思就是说在请求目标中找到无效字符,有效字符在RFC 7230和RFC 3986中定义,也就请求中包含非法字符。
三、原因分析
这里的[]和{}都是非法字符。Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证。
四、解决
第一种方法:
对于springboot而言。我们需要重新写一个配置类来让它变成合法的。
package org.jeecg.modules.jmreport.config;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author xuhc7
* @date 2022年01月10日 15:02
*/
@Configuration
public class WebConfig {
@Bean(name="webServerFactory")
public ServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory tomcatServletWebServerFactory= new TomcatServletWebServerFactory();
tomcatServletWebServerFactory.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "[]{}"));
return tomcatServletWebServerFactory;
}
}
第二种方法:
在yml中配置
server:
tomcat:
relaxedQueryChars: <,>, [,],^,`,{,|,}
五、说明
对于网上说的降低tomcat版本个人感觉浪费时间也不实用。