记一次被IE浏览器坑到的经历

项目在上线以后收到客户报过来的一个异常
在这里插入图片描述
一看到IE浏览器我心里就暗想不妙
赶紧去线上查异常日志

04-Jun-2019 11:27:27.086 INFO [http-nio-8087-exec-5] 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:479)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:684)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

一开始以为是请求头有问题,按照百度上找来的方法,修改了conf/server.xml中的配置:

<Connector port="8080" protocol="HTTP/1.1"
               maxHttpHeaderSize="8192"
               connectionTimeout="20000"
               redirectPort="8443" />

<Connector>节点中加入属性maxHttpHeaderSize="8192",重启服务,用IE浏览器测试,结果问题还是没解决。。。
莫非不是请求头的问题?

于是我又转换了方向,从网上看到了以下说法:
Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,在http解析时做了严格限制。
RFC3986文档规定,请求的Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符。

如果url中包含其他的特殊字符,就需要做一些额外的配置。
在conf/catalina.properties的最后两行添加

tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true

官方文档中对这两个配置的解释是:

  • tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
    This system property is deprecated. Use the relaxedPathChars and relaxedQueryChars attributes of the Connector instead. These attributes permit a wider range of characters to be configured as valid.

The acceptable characters for this property are: |, { , and }
WARNING: Use of this option may expose the server to CVE-2016-6816.
大概意思就是这条属性可以设置允许url路径中的三个特殊字符 |, { , 和}
如果要允许更多的特殊字符,请到relaxedPathChars和relaxedQueryChars这两个属性中设置。

  • org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
    If this is true ‘%2F’ and ‘%5C’ will be permitted as path.delimiters.If not specified, the default value of false will be used.
    翻译过来就是如果这设置为true, ‘%2F’ 和‘%5C’ 将被允许作为路径分隔符(%2F%5C/\的转码)

然后再打开conf/server.xml,修改<Connector>节点,添加

relaxedPathChars="|{}[],"
relaxedQueryChars="|{}[],"

这2个属性,可以接收任意特殊字符的组合,根据需要可以自行增减。
做好这些以后,再次重启服务,再次用IE浏览器打开网页。
还是不行啊!!!还是那个异常,还是那个配方!!!

这时候我将谷歌浏览器中打开的url和IE浏览器中的url互相复制到文本里面做对比,当复制到文本里面的时候我突然发现了问题
在这里插入图片描述
上面那是IE浏览器里的url,对比下面那行,很明显IE浏览器没有对中文做转码
于是我在js中加入编码let name = encodeURI(this.menuName)
然后修改conf/server.xml
<Connector>节点中加入属性URIEncoding="UTF-8"
最好是先把参数传换后再进行拼接。而不是把url拼接好再去转换。因为当参数中的内容为空时,encodeURI(url)有可能会把等号等一些符号转换掉

再次重启服务,打开IE。终于不报错了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值