前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java.lang.IllegalArgumentException: Invalid character found in the request target. The valid charact

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid charact

作者头像
HaC
发布2021-02-04 11:08:55
2.3K0
发布2021-02-04 11:08:55
举报

报错:

java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

在这里插入图片描述
在这里插入图片描述
 02-Feb-2021 09:08:32.817 信息 [http-nio-8080-exec-1] org.apache.coyote.http11.AbstractHttp11Processor.process 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.AbstractNioInputBuffer.parseRequestLine(AbstractNioInputBuffer.java:287)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1065)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
	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)

场景出现:

postman发起一个get请求,但是参数是一个特殊字符的值:

在这里插入图片描述
在这里插入图片描述

解决方法一:

把参数encode一下

在这里插入图片描述
在这里插入图片描述

对应到前端这边,就需要把 提交数据用 encodeURI(提交的数据变量)

推荐使用这种方法。

解决方法二:

不要使用get方法,使用post方法

解决方法三:

参考自: https://blog.csdn.net/ljheee/article/details/82051755

https://blog.csdn.net/qq_32365919/article/details/82055800

根本原因是 高版本tomcat中的新特性:就是严格按照 RFC 3986规范进行访问解析,而 RFC 3986规范定义了Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符(RFC3986中指定了以下字符为保留字符:! * ’ ( ) ; : @ & = + $ , / ? # [ ])。

而在这里我传入的json串 包含了 "{ }" 非法字符。

解决办法(亲测) 在conf/catalina.properties中最后添加2行: 我这里放行 三个 非法字符 |{}

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

该方式,在所有Tomcat版本均适用。 解释:在执行请求时,碰到该没问题的,大多是在URL中传输Json等。尤其是用浏览器地址输入的URL死活不能包含有任何特殊字符。否则会返回400 状态码。 首先:不推荐降低tomcat版本,这等于掩耳盗铃,绝对得不偿失。 Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,在http解析时做了严格限制。

如果你仅仅 需要使用这三个字符,配置到此就OK了。重启tomcat,rebuild工程再启动,就能正常使用了。

但!

不幸的是, requestTargetAllow 只能配置|、{、} 允许这三个字符,对于其他的( 例如" < > [ \ ] ^ ` { | } .),在请求时,仍然拦截,如果使用了 |{} 之外的其他字符那怎么办呢?那就还需要如下配置:

conf/server.xml中的<Connector>节点中,添加2个属性:

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

这种方法上线了还要修改Tomcat,得不偿失。

建议使用第一种方法,前端使用 encodeURI() 方法。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-02-02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 场景出现:
  • 解决方法一:
  • 解决方法二:
  • 解决方法三:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档