url使用|导致The valid characters are defined in RFC 7230 and RFC 3986

背景

把springboot1.4.3.RELEASE版本升级到2.0.2.RELEASE,一个GET请求

1
2
3
4
5
6
7
8
9
10
11
12
```yaml
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) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:687) ~[tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_101]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]

原因

tomcat7.0.73之后对url进行了更严格的验证。如果使用非保留字符,就会报Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

RFC3986 中指定了以下字符为保留字符: ! * ‘ ( ) ; : @ & =+ $ , / ? # [ ]

解决

  • 使用允许的符号替换
  • url 编码
  • 使用更低版本的tomcat
  • HttpParser中设置relaxedPathCharsrelaxedQueryChars

我这边是换了另外一个符号替换|了。

参考:

https://bz.apache.org/bugzilla/show_bug.cgi?id=60594
http://tomcat.apache.org/tomcat-8.0-doc/config/systemprops.html#Other
https://tools.ietf.org/html/rfc3986
https://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid/13500078#13500078


url使用|导致The valid characters are defined in RFC 7230 and RFC 3986
https://www.wekri.com/tomcatRFC7230AndRFC3986/
Author
Echo
Posted on
May 25, 2018
Licensed under