终极解决方案:

Invalid character found in the request target. The valid characters are defined in RFC 3986

25-Aug-2018 11:27:27.086 信息 [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)

从错误日志中看到Error parsing HTTP request header

负责解析http请求的是org.apache.tomcat.util.http.parser.HttpParser,它对请求对URL中对字符做了限制,具体代码如下:

IS_NOT_REQUEST_TARGET[]中定义了一堆not request target

if(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;
}

转换过来就是以下字符(对应10进制ASCII),也就是URL中不能包含的特殊字符:

键盘上那些控制键:(<32或者=127)
非英文字符(>127)
空格(32)
双引号(34)
#(35)
<(60)
>(62)
反斜杠(92)
^(94)
TAB上面那个键,~(96)
{(123)
}(124)
|(125)

我碰到这个问题也捣弄了半天,参照其他的各种解决方案都不行。并且我是使用了多种特殊字符,运用单一某种方案还不能完全奏效。下面是经过实践的解决方案,请按顺序读完。

解决办法(亲测)

在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解析时做了严格限制。

RFC3986文档规定,请求的Url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~4个特殊字符以及所有保留字符。

如果要使用这些特殊字符,并不是绝对不可以。Tomcat做了限制的同时,也提供了相关配置。给出的解决方案第一行requestTargetAllow,指定了允许的特殊字符,在等号后面配|{}就行了。

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

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

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

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

这2个属性,可以接收任意特殊字符的组合,根据需要可以自行增减。

与此相关的文档说明:

讲解:tomcat.util.http.parser.HttpParser. requestTargetAllow

文档地址:https://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html

讲解:relaxedPathChars,relaxedQueryChars

文档地址:https://tomcat.apache.org/tomcat-8.5-doc/config/http.html

<Connector port="80"
protocol="HTTP/1.1"
maxThreads="150"
connectionTimeout="20000"
redirectPort="443"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml"
relaxedPathChars="|{}[],"
relaxedQueryChars="|{}[],"
/>

Tomcat 8 Invalid character found in the request target. The valid characters are defined in RFC 3986的更多相关文章

  1. 异常:Invalid character found in the request target. The valid characters are defined in RFC 3986

    一.背景 事情是这样的,前几天做一个基本的数据库“增删改查”的需求,前端传参的方式是“JSON字符串”,后端接收到此参数后,使用阿里巴巴fastjson进行解析,然后入库.需求很简单吧,但是偏偏遇到问 ...

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

    十二月 , :: 下午 org.apache.coyote.http11.AbstractHttp11Processor process 信息: Error parsing HTTP request ...

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

      1.情景展示 tomcat 日志时不时会报出如下异常信息,到底是怎么回事? java.lang.IllegalArgumentException: Invalid character found ...

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

    问题描述:后台报错 Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.java ...

  5. Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC

    解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RF ...

  6. 解决springboot项目请求出现非法字符问题 java.lang.IllegalArgumentException:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    springboot版本: 2.1.5 最近使用springboot搭建了一个App后台服务的项目,开发接口的时候在本机使用postman工具做请求测试,请求返回一直很正常,但是在前端开发使用h5请求 ...

  7. 已解决:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 问题

    请求: http://127.0.0.1:8080/driverApp/findLikeAddress?json={"shopname":"广东省"," ...

  8. IE浏览器连接WebSocket报错:java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    在项目开发中整合了WebSocket,本来没什么问题了,但是偶尔发现用IE浏览器打开web端不能推送消息,因为PC端与服务器建立连接失败了.网上查了很多资料, 又看了看源码,都不对症:又怀疑是Spri ...

  9. IE浏览器报400错误:Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    前言: 在用IE浏览器时访问tomcat项目时,页面报400错误,后台错误: java.lang.IllegalArgumentException: Invalid character found i ...

随机推荐

  1. (办公)记事本_linux网络命令

    参考谷粒学院的linux视频教程:http://www.gulixueyuan.com/course/300/task/7091/show 阿里云ECS云服务器更换公网IP的方法:https://yq ...

  2. Go 语言 fmt.Sprintf (格式化输出)

    Printf().Sprintf().Fprintf() 函数的区别用法是什么? 都是输出格式化字符串,只是输出到的目标不一样: Printf() 是把格式化字符串输出到标准到标准输出(一般是屏幕,可 ...

  3. Block Chain Learning Notes

    区块链是什么 区块链技术是由比特币创造的,本文也将从比特币开始进行引导,一步一步告诉大家什么是区块链.如果你想立马知道区块链是什么,也可以直接转到文章末尾的区块链定义. 区块链,可能是当下最有前景又充 ...

  4. gRPC Learning Notes

    简介 更多内容参考:https://www.grpc.io/docs/guides/ gRPC 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 G ...

  5. Java 使用代理发送Http请求 (将Http请求代理Https请求)

    package com.test.porxy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  6. LaunchPad

    链接:https://ac.nowcoder.com/acm/contest/3665/D来源:牛客网 Hery is a boy with strong practical abilities. N ...

  7. 开放应用模型操作指南(一)| 云服务一键接入 OAM 体系

    作者 | 邓洪超  阿里云容器平台软件工程师 导读:Open Application Model(OAM)是阿里云联合微软等国际顶级技术团队联合发布的开放应用模型技术.旨在通过全新的应用定义.运维.分 ...

  8. 转:建立maven私服

    一.下载安装与配置 下载 到官网下载:https://www.sonatype.com/download-oss-sonatype   image.png 下载的是oss3.x版本的(当时最新版), ...

  9. pdf.js的使用(2)新的需求已经出现,怎么能够停止不前(迪迦奥特曼主题曲)哈哈哈。^_^

    来,咱们看图说事 按钮1,2是pdf.js自带的,分别对应顺时针旋转90度,逆时针旋转90度.于是乎又要我做一个旋转180度的按钮,诺!按钮3来了. 1.别怂,干!首先顺藤摸瓜,看按钮1,2的html ...

  10. 吴裕雄--天生自然Numpy库学习笔记:NumPy Ndarray 对象

    NumPy 最重要的一个特点是其 N 维数组对象 ndarray,它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引. ndarray 对象是用于存放同类型元素的多维数组. ndarr ...