Java SpringBoot - 解析 HTTP 请求标头时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49273847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpringBoot - Error parsing HTTP request header
提问by Misho Janiashvili
I am running SpringBoot Application
just checked server logs and got several errors like this. I can't understand what can cause it as the error appears everyday after 12/24 hours.
我正在运行SpringBoot Application
刚刚检查过的服务器日志,并遇到了几个这样的错误。我不明白是什么原因造成的,因为错误在 12/24 小时后每天都会出现。
Tomcat Version running on 8.5.11
Tomcat 版本运行于 8.5.11
2018-03-04 17:03:26 [http-nio-8080-exec-85] INFO o.a.coyote.http11.Http11Processor - 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 method name. HTTP method names must be tokens
at org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:421)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:667)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
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)
回答by coffman21
This may happen because of parsing HTTPS headers instead of HTTP. Try:
这可能是因为解析 HTTPS 标头而不是 HTTP。尝试:
- Adding:
logging.level.org.springframework.web: trace
logging.level.org.apache: trace
to your application.properties and see what does Spring says to you. - Check if there are any scheduled activity at that time which refers to other resource encrypted by SSL. See also: java.lang.IllegalArgumentException: Invalid character found in method name. HTTP method names must be tokens
- 添加:
logging.level.org.springframework.web: trace
logging.level.org.apache: trace
到您的 application.properties 并查看 Spring 对您说什么。 - 检查当时是否有任何计划活动,这些活动涉及由 SSL 加密的其他资源。另请参阅:java.lang.IllegalArgumentException:在方法名称中发现无效字符。HTTP 方法名称必须是令牌
回答by sigint
I had this error in a Spring Boot 2 (2.0.1.RELEASE) application that was configured to serve HTTPS on port 8443 and redirect port 8080 HTTP traffic to port 8443.
我在 Spring Boot 2 (2.0.1.RELEASE) 应用程序中遇到此错误,该应用程序配置为在端口 8443 上提供 HTTPS 并将端口 8080 HTTP 流量重定向到端口 8443。
On Microsoft Edge, the application worked as expected, with http://localhost:8080
redirecting to https://localhost:8443
. On Chrome 66 however, this would only work once, and then Chrome would complain that "localhost sent an invalid response" (ERR_SSL_PROTOCOL_ERROR
).
在 Microsoft Edge 上,应用程序按预期工作,http://localhost:8080
重定向到https://localhost:8443
. 然而,在 Chrome 66 上,这只能工作一次,然后 Chrome 会抱怨“本地主机发送了无效响应”(ERR_SSL_PROTOCOL_ERROR
)。
The server log said:
DEBUG 11440 --- [nio-8080-exec-1] o.a.coyote.http11.Http11InputBuffer: Received [ <<unprintable characters>> ]
INFO 11440 --- [nio-8080-exec-1] o.apache.coyote.http11.Http11Processor: Error parsing HTTP request header
服务器日志说:
DEBUG 11440 --- [nio-8080-exec-1] o.a.coyote.http11.Http11InputBuffer: Received [ <<unprintable characters>> ]
INFO 11440 --- [nio-8080-exec-1] o.apache.coyote.http11.Http11Processor: Error parsing HTTP request header
It turns out that Chrome was adding localhost
to its HSTS listbecause Spring Boot sent back a Strict-Transport-Security: max-age=31536000 ; includeSubDomains
header back for
https://localhost:8443. So essentially, this issue happened because the client (i.e., browser) was trying to speak HTTPS to an HTTP endpoint.
事实证明,Chrome 正在将localhost
其添加到其HSTS 列表中,因为 Spring Boot 发回了https://localhost:8443的Strict-Transport-Security: max-age=31536000 ; includeSubDomains
标头
。所以本质上,这个问题的发生是因为客户端(即浏览器)试图通过 HTTPS 与 HTTP 端点通信。
Adding a .headers().httpStrictTransportSecurity().disable();
in
<? extends WebSecurityConfigurerAdapter>.configure
fixed the issue, as noted in this StackOverflow question.
添加一个.headers().httpStrictTransportSecurity().disable();
in
<? extends WebSecurityConfigurerAdapter>.configure
修复了该问题,如此 StackOverflow question 中所述。
回答by Gondy
As I have answered in this similar question, check if you are not accidentally requesting with HTTPS protocol instead of HTTP protocol. If you don't configure SSL on Tomcat and you send HTTPS request, it will result to this weird message..
正如我在这个类似问题中回答的那样,检查您是否不小心使用 HTTPS 协议而不是 HTTP 协议进行请求。如果你没有在 Tomcat 上配置 SSL 并且你发送 HTTPS 请求,它会导致这个奇怪的消息..
回答by Rookie_Guy
Tell you what it solves when I used Firefox instead of chrome.
告诉你当我使用 Firefox 而不是 chrome 时它解决了什么问题。