公司tomcat從7.0.91更換到高版本的9.0.22以后,postman請求沒問題,但是終端請求總是400
報(bào)錯(cuò)Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986,
原因:
RFC 3986規(guī)范定義了Url中只允許包含英文字母(a-zA-Z)、數(shù)字(0-9)、-_.~4個(gè)特殊字符以及所有保留字符(RFC3986中指定了以下字符為保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。而我們的系統(tǒng)在通過地址傳參時(shí),在url中傳了一段json,傳入的參數(shù)中有”{“不在RFC3986中的保留字段中,所以會(huì)報(bào)這個(gè)錯(cuò)。
解決:
1、在tomcat的conf/server.xml 的Connector中添加:relaxedQueryChars="{}|[],%" relaxedPathChars="[]|{},%" 需要編碼什么特殊字符添加什么。
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000"
**relaxedQueryChars="[]|{}\**`^\`"<>`\**" relaxedPathChars="[]|{}\**`^\`"<>`\**"** redirectPort="8443" />
有中文的話加上:URIEncoding="UTF-8"
HTML5轉(zhuǎn)義字符
numeric character reference(NCR),數(shù)字取值為目標(biāo)字符的 Unicode code point;以「&#」開頭的后接十進(jìn)制數(shù)字,以「&#x」開頭的后接十六進(jìn)制數(shù)字。
https://dev.w3.org/html5/html-author/charref``
2、請求前編碼:URLEncoder.encode(param)
網(wǎng)上有些文章說在 catalina.properties 配置下面的, 其實(shí)根本沒有效果的.
org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}
這些代碼是沒有效果的.