请求的url中直接传json导致tomcat报错:Invalid character found in the request target.

tomcat报错: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986


Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
野鸡翻译:
在请求目标中发现无效的字符。请使用在RFC7230和RFC3986中定义的有效字符。

问题描述

浏览器方式:
在这里插入图片描述
postman方式:
在这里插入图片描述
两种方式是一样的只要url中的参数直接传了json都会报错。

问题原因

tomcat会将json的 ‘{’ '}'视为无效的字符,从而报错。

问题解决

第一种方法

降低tomcat版本,降到7以下,(以上问题描述中的tomcat版本为9.0.21),估摸着是在7及以上版本才添加的该限制。

第二种方法

发送http请求的时候对json参数进行url编码。
在这里插入图片描述
结果显示:未报错。

第三种方法

配置tomcat的catalina.properties 添加或者修改:

tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}

但是, 根据官方文档 tomcat7及以上中显示该属性已经被废弃了。
在这里插入图片描述
所以,这种方法行不通,使用其推荐的代替方法。即第四种方法。

第四种方法 (推荐)

使用Connector中relaxedPathChars和relaxedQueryChars属性可以解决问题.找到tomcat/conf/server.xml,在Connector中增加这两个配置.

<Connector port="8080" protocol="HTTP/1.1" relaxedPathChars="[]{}|^" relaxedQueryChars="[]{}|^" />

附:

  1. 用ajax方式发送json数据时不会存在该问题
$.ajax({
    
    
		url:"http://192.168.8.175:8088/medium",
		type:"post",
		data:{
    
    
			param1:"aaa",
			param2:"bbb",
			param3:"ccc"
		},
		dataType:"json",
		success:function(result) {
    
    	
			// doSomething
		}
	});
  1. 使用form表单提交时不会存在该问题

在这里插入图片描述

参考:https://www.dazhuanlan.com/2020/03/20/5e7404d60e4b3/

猜你喜欢

转载自blog.csdn.net/qq_29025955/article/details/115077305