解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and R

520 阅读1分钟

错误信息

29-Apr-2019 16:36:18.770 INFO [http-nio-8050-exec-5] org.apache.coyote.http11.AbstractHttp11Processor.process 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.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:287)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1065)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:622)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

参考这篇文章解决了问题。

  • 上面异常发生在前台页面向后台发送get请求的时候,错误内容说得很清楚,request中有非法字符,在RFC7230和RFC3986规范中都定义为非法字符。发生异常的web容器为tomcat7.0.75,而本地使用tomcat7.0.65时没发生报错,说明是因为更高版本的tomcat对request参数安全校验更加严格了。网上很多人把tomcat降级,其实这样不对的,我们应该找到问题产生的根本原因,上面不是说非法字符引起的吗,我们就一个个过滤,看到底是因为哪个非法字符造成的。我们只需对怀疑的字符一个个进行尝试就可以找到答案。最终,笔者找到自己发生异常的原因是因为"|"竖线这个字符引起的,当时笔者想当然认为这个字符没问题,传参时没有进行编码,没想到高版本对它进行了限制。更多的信息可以查看RFC7230和RFC3986规范。

根据上述文章所说的一个个检查,最终发现是’&'字符引起的错误。更改后解决。