解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
这个问题是高版本tomcat中的新特性:Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http请求的规则验证,就是严格按照 RFC 3986规范进行访问解析,而 RFC 3986规范定义了Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。通过地址传参时,传入的参数中有不在RFC3986中的保留字段中的字符就会报这个错。
解决方案:
1、自行重新编码请求
2、更换tomcat为较低版本
3、配置tomcat的catalina.properties
修改或添加如下配置项并添加自己需要的规则|{,},[,],\,^
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{,},[,],\,^
注意:这种方式只适合特殊字符,如果是中文此方法无效
4、配置tomcat的server.xml
tomcat官网不建议配置tomcat.util.http.parser.HttpParser. requestTargetAllow
原文建议:
This system property is deprecated. Use the relaxedPathChars
and relaxedQueryChars
attributes of the Connector instead. These attributes permit a wider range of characters to be configured as valid.
Connector修改为
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
relaxedQueryChars="{,},[,],\,^" />
经测试此方法最有效!
喜欢就点赞评论+关注吧
感谢阅读,希望能帮助到大家,谢谢大家的支持!