Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986.
在给同事导入公司项目的时候,发现了这个错误。这个错误的原因是在Tomcat7.0.69之后,版本添加了RFC 3986 对http头的验证。报错信息如下:
tomcat执行时报出的一个错误,错误信息如下:
[2017-12-11 11:42:38,010]-[http-bio-8085-exec-4]-[org.springframework.security.web.context.SecurityContextPersistenceFilter:97]-[DEBUG] -- SecurityContextHolder now cleared, as request processing completed Feb 17, 2017 11:42:39 AM org.apache.coyote.http11.AbstractHttp11Processor process INFO: 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.InternalInputBuffer.parseRequestLine(InternalInputBuffer.java:189) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1000) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
这种错误是因为RFC3986文档规定,Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(!*'();:@&=+$,/?#[])。理由如下:
->空格Url在传输的过程,或者用户在排版的过程,或者文本处理程序在处理Url的过程,都有可能引入无关紧要的空格,或者将那些有意义的空格给去掉
->引号以及<>引号和尖括号通常用于在普通文本中起到分隔Url的作用
->#通常用于表示书签或者锚点
->%百分号本身用作对不安全字符进行编码时使用的特殊字符,因此本身需要编码
->{}|\^[]`~某一些网关或者传输代理会篡改这些字符
解决办法很简单,处理一下不安全字符:
var url="otherRequest.jsp?sAct=setFAVDefault"+"&usrid="+usrid+"&mnutyp="+mnutyp+"&mnudes="+mnudes+"&txname="+txname+"&link="+link; url=encodeURI(url);