Tomcat中Invalid character found in the request target. The valid characters are defined in RFC 7230解决
关于Tomcat中Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC xxxx的问题解决方案
一. 异常现象
我在编写SpringMVC传参案例时,通过浏览器的url地址栏给后端的数组或集合类型对象传递参数,url参数中带有”[]“符号,如下所示:
结果请求后产生了如下异常提示信息:
Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
[http-nio-8087-exec-5] org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header
Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:479)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:684)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
二. 异常原因
从错误日志中看到Error parsing HTTP request header 负责解析http请求的是org.apache.tomcat.util.http.parser.HttpParser,它对请求对URL中对字符做了限制,具体代码如下: IS_NOT_REQUEST_TARGET[]中定义了一堆not request target。
if(IS_CONTROL[i] || i > 127 || i == 32 || i == 34 || i == 35 || i == 60 || i == 62 || i == 92 || i == 94 || i == 96 || i == 123 || i == 124 || i == 125) {
IS_NOT_REQUEST_TARGET[i] = true;
}
这个问题是因为高版本的tomcat中增加了新特性:
就是严格按照 RFC 3986规范进行访问解析,而 RFC 3986规范定义了Url中只允许包含英文字母,如下:
(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。
而我们的系统在通过地址传参时,在url中传了一段json,传入的参数中有”{“不在RFC3986中的保留字段中,所以会报这个错。
所以简单点说,就是我们的URL中不能包含如下特殊的字符:
键盘上那些控制键:(<32或者=127)
非英文字符(>127)
空格(32)
双引号(34)
#(35)
<(60)
>(62)
反斜杠(92)
^(94)
TAB上面那个键,~(96)
{(123)
}(124)
|(125)
三. 解决办法
1. 首先找到自己的tomcat安装目录
2. 修改配置文件
2.1 在conf/catalina.properties中最后添加2行:
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
如下图所示:
2.2 在conf/server.xml中的<Connector>节点中,添加2个属性:
relaxedPathChars="|{}[],"
relaxedQueryChars="|{}[],"
如下图所示:
然后重启自己的项目即可。
上一篇: Feign远程调用丢失请求头问题
下一篇: Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration 异常解决
推荐阅读
-
Tomcat中Invalid character found in the request target. The valid characters are defined in RFC 7230解决
-
tomcat异常解决(Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986)
-
解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and ..
-
解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 问题
-
tomcat异常解决(Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986)