問題背景:
由于需要自己寫了壓縮和解壓某個對象的互轉(zhuǎn),壓縮前為json格式的數(shù)據(jù)儒溉,方便修改也方面查看歹苦,壓縮后是Deflater壓縮后的數(shù)據(jù),然后在用URLEncoder編碼后的數(shù)據(jù)(由于自己用了get請求边灭,再編碼是為了讓url能認識)。
在壓縮的時候報錯:
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規(guī)范進行訪問解析健盒,而 RFC 3986規(guī)范定義了Url中只允許包含英文字母(a-zA-Z)绒瘦、數(shù)字(0-9)、-_.~4個特殊字符以及所有保留字符(RFC3986中指定了以下字符為保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])扣癣。傳入的參數(shù)中有"{"不在RFC3986中的保留字段中惰帽,所以會報這個錯。
解決:
Tomcat 7.0.76, 8.0.42, 8.5.12 這些版本之后可以定義requestTargetAllow 屬性來允許禁止的字符父虑。在tomcat的 catalina.properties文件中添加這一句:
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
如果是springboot工程该酗,可以在SpringBootApplication的的main方法中增加
System.setProperty("tomcat.util.http.parser.HttpParser.requestTargetAllow","|{}");
補充:
部署到tomcat運行到時候,tomcat8.5.30是沒有問題的,tomcat8.5.33卻出現(xiàn)了同樣到問題。
去了官網(wǎng)文檔查看說:tomcat.util.http.parser.HttpParser. requestTargetAllow(This system property is deprecated. Use the relaxedPathChars and relaxedQueryChars attributes of the Connector instead)
然后修改server.xml文件呜魄,Connector屬性修改為如下:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" relaxedQueryChars="[]|{}^\`"<>" redirectPort="8443" />