Spring启动http响应压缩对某些用户代理不起作用 [英] Spring boot http response compression doesn't work for some User-Agents
问题描述
我尝试在Spring启动Web应用程序上启用http响应压缩。
它适用于一些用户代理,并且由于某种原因对其他人不起作用(下面的具体情况)。
我的基本问题是:
为什么Spring Boot中的http响应压缩(gzip)仅适用于某些User-Agent标头及其配置位置。
我准备了简单的网络应用程序,并启用了压缩功能: sample spring-boot-compression app
有验证gzip编码只适用于某些情况的集成测试。
我配置的春天启动与:
服务器:
tomcat:
压缩:on
compressable-mime-types:text / html,text / css,application / javascript,application / json,application / font-sfnt,application / font-woff,application / font-woff2
当我尝试使用curl做一些请求时:
code> $ curl -i -HAccept-Encoding:gzip,deflatehttp:// localhost:8080
HTTP / 1.1 200 OK
服务器:Apache-Coyote / 1.1
Content-Type:application / json; charset = UTF-8
Transfer-Encoding:chunked
Content-Encoding:gzip
Vary:Accept-Encoding
我看到 Content-Encoding:gzip
b
$ b
当我将 User-Agent
设置为AppleWebKit(或某些其他浏览器,如IE)时,它不会压缩:
$ curl -i -HAccept-Encoding:gzip,deflate-HUser-Agent:AppleWebKithttp:// localhost:8080
HTTP / 1.1 200 OK
服务器:Apache-Coyote / 1.1
Content-Type:application / json; charset = UTF-8
Transfer-Encoding:chunked
Vary :Accept-Encoding
我重复我的测试与一些其他浏览器和 User-代理
头修改并收到一些奇怪的结果。
这里有一些工作(响应被压缩) User- / code> headers:
- Mozilla / 5.0
- Mozilla / WOW64)Chrome / 46.0.2490.80 Safari / 537.36
- Mozilla / 5.0(Windows NT 6.1; WOW64; rv:34.0)Firefox / 34.0
- SomeUnknownBrowser
不工作 User-Agent
标头:
- AppleWebKit
pre>
- Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,类似Gecko)Chrome / 46.0。 2490.80 Safari / 537.36
- Mozilla / 5.0(Windows NT 6.1; WOW64; rv:34.0)Gecko / 20100101 Firefox / 34.0
- Gecko / 20100101
我也尝试使用压缩
GzipFilter
,它的行为是完全一样的。也尝试嵌入Jetty而不是Tomcat - 相同的结果。
也许我只是缺少一些东西。
您可能使用某种防病毒(可能是ESET)。尝试关闭HTTP保护。
使用Eset,您可以尝试类似:高级 - >互联网和电子邮件 - > Web访问保护 - 关闭。
I'm trying to enable http response compression on Spring boot web application. It works for some user-agents, and for some reason doesn't for others (specific cases below).
My basic question is: Why http response compression (gzip) in Spring Boot works only for some User-Agent headers and where it is configured.
Spring boot reference doesn't say anything about it.
I prepared simple web application with enabled compression: sample spring-boot-compression app There are integration tests that verify that gzip encoding works for some cases only.
I configured spring boot with:
server: tomcat: compression: on compressable-mime-types: text/html,text/css,application/javascript,application/json,application/font-sfnt,application/font-woff,application/font-woff2
When I try to do some requests with curl:
$ curl -i -H "Accept-Encoding: gzip,deflate" http://localhost:8080 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Content-Encoding: gzip Vary: Accept-Encoding
I see that
Content-Encoding: gzip
header is set.When I set
User-Agent
to AppleWebKit (or some other browsers like IE) it does not compress:$ curl -i -H "Accept-Encoding: gzip,deflate" -H "User-Agent: AppleWebKit" http://localhost:8080 HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Vary: Accept-Encoding
I repeated my tests with some other browsers and
User-Agent
header modification and received some strange results.Here some of working (response is compressed)
User-Agent
headers:- Mozilla/5.0 - Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/46.0.2490.80 Safari/537.36 - Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Firefox/34.0 - SomeUnknownBrowser
Some of not working
User-Agent
headers:- AppleWebKit - Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36 - Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0 - Gecko/20100101
I also tried to use compression with
GzipFilter
and it behaves exactly the same. Also tried embedded Jetty instead of Tomcat - same result.Maybe I'm just missing something.
解决方案You're probably using some sort of antivirus (maybe ESET). Try turning off HTTP protection.
With Eset you can try something like: Advanced -> Internet and email -> Web Access Protection - turn off.
这篇关于Spring启动http响应压缩对某些用户代理不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文