项目从JDK1.6升级到JDK1.8.172后,报java.lang.IllegalArgumentException: Invalid character found in the request

`21-Apr-2018 17:17:02.788 信息 [http-nio-8080-exec-8] 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
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:476)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:687)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol C o n n e c t i o n H a n d l e r . p r o c e s s ( A b s t r a c t P r o t o c o l . j a v a : 790 ) a t o r g . a p a c h e . t o m c a t . u t i l . n e t . N i o E n d p o i n t SocketProcessor.doRun(NioEndpoint.java:1459)
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 W o r k e r . r u n ( T h r e a d P o o l E x e c u t o r . j a v a : 624 ) a t o r g . a p a c h e . t o m c a t . u t i l . t h r e a d s . T a s k T h r e a d WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

错误及环境描述:
项目用的Spring+ struts2+Jquery LigerUI ;

> //编辑
>  function findObj(hostip){    
>  window.location.href =contextPath+"/manager/authmgr/server/authServer!find.action?hostipaddr="
> + hostip + "&curPage=${html_page_info.curPage}&talRow=${html_page_info.totalRow}"; 

js形式往后台发送编辑请求
错误原因
当在浏览器中访问时 URL中带有特殊字符,如花括号冒号时,就会出现这个错误。

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

解决方法:
原因已经清楚了,但解决方法有多种:

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

2、特殊字符是出现在后面跟的参数中,对参数进行 URL 编码,可以使用 JavaScript 中的 encodeURIComponent() 函数。

3、使用 Post 方法提交数据

以下两种方法参考:http://blog.csdn.net/zeroso/article/details/70592179

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

5、在 conf/catalina.properties 中最后添加一行:

org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

官方指南地址:http://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html

官方说明:

org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH

If this is true ‘%2F’ and ‘%5C’ will be permitted as
path.delimiters.If not specified, the default value of false will be
used.

(我一开始用了第五种解决办法,还是不好使,难过-),继续查找资料:

查资料了解到最新的tomcat6 7 8 都有这个问题,这个问题是由于tomcat的新版本增加了一个新特性,就是严格按照 RFC
3986规范进行访问解析,而 RFC
3986规范定义了Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:!
* ’ ( ) ; : @ & = + $ , / ? # [ ])。

用Fiddler抓包发现请求链接都是400的状态,就是请求参数有误。请求链接中都包含有一个参数名为值为 v e r s i o n ( h t t p : / / 127.0.0.1 : 8080 / s o l r / c s s / s t y l e s / c o m m o n . c s s ? = {version})。单独请求这个链接同样访问失败,将?= v e r s i o n 访 R F C 3986 {version}中的一对大括号不在 RFC 3986规范定义的保留字符中。

此时问题已经找到了,就是因为这对大括号引起的错误。这时候的解决方案应是对url中的非法字符进行编码。手动把一对大括号进行编码后链接为:http://127.0.0.1:8080/solr/css/styles/common.css?_=${version}
经测试,该链接可正常访问。接下来要修改源码中的过滤器,对所有请求链接进行UrlEncode编码。

果断修改代码:

window.location.href = contextPath+"/manager/authmgr/server/authServer!find.action?hostipaddr=" + hostip + "&curPage=${html_page_info.curPage}&talRow=${html_page_info.totalRow}";

重新部署启动tomcat,编辑问题解决,perfect!!

猜你喜欢

转载自blog.csdn.net/java001122/article/details/80031800