- 報錯信息如下:
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
Tomcat在 7.0.73, 8.0.39, 8.5.7 等版本后(詳情:https://stackoverflow.com/questions/41053653/tomcat-8-is-not-able-to-handle-get-request-with-in-query-parameters/44005213#44005213),添加了對于http頭的驗證,就是添加了些規則去限制HTTP頭的規范性(詳情:http://www.lxweimin.com/p/1c870461fa41)
- 根據rfc規范(RFC 3986規范定義了Url中只允許包含英文字母(a-zA-Z)、數字(0-9)、-_.~4個特殊字符以及所有保留字符(RFC3986中指定了以下字符為保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ]))。
- url中不允許有 |,{,}等特殊字符,但在實際生產中還是有些url有可能攜帶有這些字符,特別是|還是較為常見的。在tomcat升級到7以后,對url字符的檢查都變嚴格了,如果出現這類字符,tomcat將直接返回400狀態碼。
- 修改tomcat的配置,在catalina.properties添加下面的配置,重啟服務器即可。
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
tomcat官方說明
http://tomcat.apache.org/tomcat-7.0-doc/config/systemprops.html;
https://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html源碼片段如下:
if (IS_CONTROL[i] || i > 127 ||
i == ' ' || i == '\"' || i == '#' || i == '<' || i == '>' || i == '\\' ||
i == '^' || i == '`' || i == '{' || i == '|' || i == '}') {
IS_NOT_REQUEST_TARGET[i] = true; // reject the character!