Tomcat 8.5.x 400错误:Invalid character found in the request target

最近在测试中发现,在传递json字符串时,一直报400错了,具体的错误信息如下:

23-Sep-2020 11:35:33.477 信息 [http-nio2-8080-exec-2] 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:472)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:683)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
        at org.apache.tomcat.util.net.Nio2Endpoint$SocketProcessor.doRun(Nio2Endpoint.java:1697)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at org.apache.tomcat.util.net.AbstractEndpoint.processSocket(AbstractEndpoint.java:934)
        at org.apache.tomcat.util.net.Nio2Endpoint$Nio2SocketWrapper$4.completed(Nio2Endpoint.java:626)
        at org.apache.tomcat.util.net.Nio2Endpoint$Nio2SocketWrapper$4.completed(Nio2Endpoint.java:604)
        at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
        at sun.nio.ch.Invoker$2.run(Invoker.java:218)
        at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
        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)
        at java.lang.Thread.run(Thread.java:745)

网上给出了好多解决办法,最好的解决办法就是在server.xml中设置如下:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" relaxedQueryChars="[]|{}^&#x5c;&#x60;&quot;&lt;&gt;" />

记加入relaxedQueryChars参数,可是可是,我的生产环境就是不生效。开始以为是配置的不正确,于是
查看了生产环境tomcat的版本是8.5.13,于是我本地也安装了8.5.13的tomcat,确实是不起作用。但是我本地开发用的tomcat 8.5.34,加入了上面的配置以后,是生效的。那么 ,我就怀疑是于小版本有问题,果然,在 62273 – Add support for alternate URL specification 看到,社区只在当时的各个大本版的当前版本做了修正:

Adding extra code to Tomcat to account for specification non-compliance of other components is the wrong solution. The right solution is to open bugs against the non-compliant components. Unfortunately, in this case, those other components are all the major browser vendors and they do not accept that their behaviour is incorrect. I have yet to see a convincing argument as to why the browsers should not implement RFC 7230 and RFC 3986.

Working around the specification non-compliant browser behaviour just encourages vendors to continue to ignore specifications and leads to greater interoperability issues in the long term. However, the alternative is to break lots of applications for lots of users. Therefore, it is with regret that I have implemented this enhancement for all currently supported Tomcat versions.

Fixed in:
- trunk for 9.0.8 onwards
- 8.5.x for 8.5.31 onwards
- 8.0.x for 8.0.52 onwards
- 7.0.x for 7.0.87 onwards

即:

> 9.0.8
> 8.5.31
> 8.0.52
> 7.0.87

所以,我直接更换了最新的tomcat稳定版8.5.58,问题解决。


原文:Tomcat 8.5.x 400错误:Invalid character found in the request target_泗水长流的博客-CSDN博客_tomcat8.5 400错误
作者: 泗水长流