Spring boot http response compression doesn't

2019-03-29 23:33发布

问题:

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 setUser-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.

回答1:

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.



回答2:

This worked for me with Spring 1.4

 server.compression.enabled: true
server.compression.mime-types: application/json,application/xml,text/html,text/xml,text/plain,text/css,application/javascript