已解決:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 問題

於生發表於2020-08-04

請求:

http://127.0.0.1:8080/driverApp/findLikeAddress?json={"shopname":"廣東省","uid":1001}

控制檯報錯:

04-Aug-2020 12:03:09.642 資訊 [http-nio-8080-exec-8] org.apache.coyote.http11.Http11Processor.service 解析 HTTP 請求 header 錯誤
 Note: further occurrences of HTTP request 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:483)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:502)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:818)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)
 

問題:Tomcat丟擲異常,導致400 –錯誤請求。

從7.0.88開始,大多數tomcat版本都會出現此問題。

解決方案:(由Apache團隊建議):

Tomcat增強了其安全性,並且不再允許在查詢字串中使用原始方括號。在請求中,我們有{,} 或其他符號,因此伺服器未處理該請求。

在tomcat conf路徑下的server.xml檔案裡面的 Connector 標籤下新增屬性 relaxedQueryChars:

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" 
               maxHttpHeaderSize ="10240" 
               relaxedQueryChars="{,}"/>

如果應用程式需要預設情況下tomcat不支援的更多特殊字元,請在relaxedQueryChars上述屬性中新增這些特殊字元,並以逗號分隔。

相關文章