解决:Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986

问题描述

在tomcat9.0下请求数据时,由于使用了JSON类型的字符串,发现请求错误,查询日志发现是有些地址直接被tomcat认为存在不合法字符,返回HTTP 400错误响应,错出信息如下:

产生原因

经百度了解到,这个问题是高版本tomcat中的新特性:就是严格按照 RFC 3986规范进行访问解析,而 RFC 3986规范定义了Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。而我们的系统在通过地址传参时,在url中传了一段json,传入的参数中有"{"不在RFC3986中的保留字段中,所以会报这个错。

解决方案

网友给出的解决方案大致可分为三种:

1.降低tomcat版本至7以下,很明显,这种方法不是大家想要的。

2.修改tomcat配置

.../conf/catalina.properties中,找到最后注释掉的一行 #tomcat.util.http.parser.HttpParser.requestTargetAllow=|  ,改成tomcat.util.http.parser.HttpParser.requestTargetAllow=|{},表示把{}放行,若无#tomcat.util.http.parser.HttpParser.requestTargetAllow=|   ,可直接添加tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}。

注:该方法本次实验未成功

3.在客户端手动对请求链接进行编码

如:

encodeURI("http://localhost:8080/app/handleResponse?msg=name|id|")

> http://localhost:8080/app/handleResponse?msg=name%7Cid%7C

或者:

encodeURIComponent("msg=name|id|")

> msg%3Dname%7Cid%7C

建议使用第三中方式

 

参考:

https://www.cnblogs.com/wsygdb/p/7661220.html

https://stackoverflow.com/questions/41053653/tomcat-8-is-not-able-to-handle-get-request-with-in-query-parameters

 

posted @ 2018-10-29 21:35  sck_dream  阅读(690)  评论(0编辑  收藏  举报