后台报错java.lang.IllegalArgumentException: Invalid character found in the request target.

报错:

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……

 

错误原因:

当在浏览器中访问时 URL 中带有特殊字符,如花括号冒号时,就会出现这个错误。

例如:http://localhost:8080/index.do?{id:123}

解决方法:

1、去除 URL 中的特殊字符;

3、使用 Post 方法提交数据

4、更换低版本的 Tomcat 来规避这种问题。

5、在 conf/catalina.properties  添加或者修改: 

  5.1  添加  tomcat.util.http.parser.HttpParser.requestTargetAllow=|{} 

5.2  修改 tomcat/conf/catalina.properties 的配置文件
Tomcat 在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于 http 头的验证。
具体来说,就是添加了些规则去限制 HTTP 头的规范性
org.apache.tomcat.util.http.parser.HttpParser#IS_NOT_REQUEST_TARGET[] 中定义了一堆 not request target
if(IS_CONTROL[i] || i > 127 || i == 32 || i == 34 || i == 35 || i == 60 || i == 62 || i == 92 || i == 94 || i == 96 || i == 123 || i == 124 || i == 125) {
IS_NOT_REQUEST_TARGET[i] = true;
}
转换过来就是以下字符 (对应 10 进制 ASCII 看):
键盘上那些控制键:(<32 或者 =127)
非英文字符 (>127)
空格 (32)
双引号 (34)
#(35)
<(60)
>(62)
反斜杠 (92)
^(94)
TAB 上面那个键, 我也不晓得嫩个读 (96)
{(123)
}(124)
|(125)

 

重启服务器后,解决问题。

总结:

个人本地在 conf/catalina.properties 中添加   tomcat.util.http.parser.HttpParser.requestTargetAllow=|{} ,成功解决问题。

解决办法有很多方式,具体如下几种:

1. 遵循 7230 and RFC 3986 规范,对于非保留字字符做转义操作

2. 使用保留字字符

3. 降低 tomcat 版本

4. 将 json 数据进行 urlencode 编码

个人建议从目前的角度出发使用第三种方式降低 tomcat 版本就可以了,如果从长远出发的话,建议遵循 RFC 7230 and RFC 3986 规范,对于非保留字字符(json 格式的请求参数)做转义操作。感觉本文对自己有所帮助,欢迎在本站素文宅博客 blog.yoodb.com 留言。