問題出現的環境背景及自己嘗試過哪些方法Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了對于http頭的驗證。具體來說,就是添加了些規則去限制HTTP頭的規范性。org.apache.tomcat.util.http.parser.HttpParser#IS_NOT_REQUEST_TARGET[]中定義了一堆not request targetif(IS_CONTROL[i] || i > 127 || i == 32 || i == 34 || i == 35 || i == 60 || i == 62 || i == 92 || i == 94 || i == 96 || i == 123 || i == 124 || i == 125) {
IS_NOT_REQUEST_TARGET[i] = true;
}不太想用POST,又由于是內嵌的Tomcat,所以為了開發方便還是想通過修改springboot配置的方式去解決,剛看了下 server.tomcat.* 相關的配置,暫時沒有找到解決非法字符的問題### 問題描述<parent> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>1.5.14.RELEASE</version>-->
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository --></parent>請求URL:http://localhost/xxx/中文參數報錯:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986很多人給了解決辦法,但是還是感覺不太靈活,還是想通過springboot配置的方式,
1 回答

郎朗坤
TA貢獻1921條經驗 獲得超9個贊
對這個“中文參數”編碼即可。
對于get請求
如果你的請求是get,參數有中文的,比如 ?name=張上,按照如下處理
前端使用 encodeURIComponent(encodeURIComponent(url)),對url進行二次編碼。
后端:拿到參數值,使用URLDecoder.decode(s, "UTF-8")解碼一下。這樣方式確實可行的,答主在實際項目總使用過。
舉個例子吧:
前端:
<script?type="text/javascript"> ???var?url?=?"/xxx/param/test";????? ?????var?name?=?"張上"; ????? ?????name?=?encodeURIComponent(name); ?????name?=?encodeURIComponent(name);//二次編碼 ?????alert(name); ?????url?=?url?+?"?name="+name;?????window.location.href?=?url;?????</script>
后端:
@Controller@RequestMapping(value="/param") public?class?ParamController?extends?BaseController<ParamEntity>?{????/** ?????*?@throws?UnsupportedEncodingException? ?????*? ?????*/ ????@RequestMapping(value="/test",method=RequestMethod.GET) ????public?String?test(@RequestParam("name")?String?name)?throws?UnsupportedEncodingException{ ????????name?=?URLDecoder.decode(name,?"UTF-8");//實測,可以正確的得到中文。 ????????System.out.println(name);????????return?"index"; ????} }
注意:get請求有中文參數,可以指定容器使用何種編碼規則來解碼提交的參數(有人回答使用這種方式,即修改tomcat 配置文件中的Connector中的URIEncoding參數),但是這種做法不建議使用,推薦使用二次編碼吧。
- 1 回答
- 0 關注
- 5826 瀏覽
添加回答
舉報
0/150
提交
取消