tomcat高版本之URL解析异常解决
IllegalArgumentException
一、项目场景:
例如:由于Apache的tomcat的版本升级,遵循RFC 7230 and RFC 3986规范解析请求地址。同时添加了对于http头的验证请求。
导致报文存在导致特殊字符(不在解析范围内的)tomcat7.0.65以上的版本会出现java.lang.IllegalArgumentException。
请求报文如下:
http://127.0.0.1:8180/appname/doTestTransaction.action?reqJson={"app.xxx":"xxx","app.xxx":"xxx","app.xxx":"xxx",""app.xxx":"xxx"":[{"app.xxx":"xxx","app.xxx":"xxx","app.xxx":"xxx"}]}
二、问题描述:
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:476) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:687) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.28.jar:8.5.28]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_161]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.28.jar:8.5.28]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
原因分析:
tomcat高版本严格按照RFC 3986规范解析地址。
该规范只允许包含 a-zA-Z 0-9 - _ . ~ 以及所有保留字符 ! * ’ ( ) ; : @ & = + $ , / ? # [ ]
另高版本的tomcat添加了对于http头的验证。
因为我的请求报文中出现了[]、{},所以使用高版本的会报错
解决方案:
方案一:替换低版本(<=7.0.65)的tomcat
方案二:
Tomcat 7.0.76, 8.0.42, 8.5.12 这些版本之后可以定义requestTargetAllow 属性来允许禁止的字符。
在tomcat的 catalina.properties文件中添加这一句:tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}表示放行{}
补充:7.0.76之后的版本针对上面的配置不建议使用,已经废弃。(可参考官方说明)
继方案二(最终解决):
去了官网文档查看说:tomcat.util.http.parser.HttpParser. requestTargetAllow
(This system property is deprecated. Use the relaxedPathChars and relaxedQueryChars attributes of the Connector instead)
连接器配置relaxedPathChars 和relaxedQueryChars 参考(官方文档)如下:
最终 在./conf/service.xml中添加realxedQueryChars属性:
onnector port="8080" protocol="HTTP/1.1"
connectionTimeout="30000"
maxThreads="800"
minSpareThreads="50"
redirectPort="8443"
relaxedQueryChars="[,],|,{,},^,\,`,",<,>"/>
来源:https://blog.csdn.net/listeningsea/article/details/121395266