angularjs Gzip压缩不工作在我的项目与Spring Boot 1.5.10.RELEASE

iqih9akk  于 2022-10-31  发布在  Angular
关注(0)|答案(2)|浏览(152)

我使用的是Sping Boot 1.5.10.RELEASE版本。Gzip压缩不工作。
您可以在本地主机上运行。
Angularjs & rest api在不同的端口上。已启用CrossOrigin以接受来自Angularjs ui的请求。
正在使用嵌入式Tomcat服务器部署Sping Boot 应用程序。未使用http 2属性,即server.http2.enabled=true
Angualrjs调用rest api下面是$http服务

$http({
  method: method,
  url: url,
  params: params,
  data: body,
  headers: {
    Authorization: token,
    "Content-type": 'application/json'
  }
});

Rest API响应大小约为25 MB,因此我希望压缩响应。
我已经在application.properties中添加了众所周知的属性,以应用gzip压缩。Spring boot 1.5.10 Supported properties


# Enable response compression

server.compression.enabled=true

# The comma-separated list of mime types that should be compressed

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

# Compress the response only if the response size is at least 1KB

server.compression.min-response-size=1024

我已观察到网络选项卡,但未观察到内容编码:gzip在响应标头中。

请求

Request URL: http://localhost:9081/employee
Request Method: GET
Status Code: 200 
Remote Address: [::1]:9081
Referrer Policy: no-referrer-when-downgrade

回应信头

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:7000
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json;charset=UTF-8
Date: Sun, 28 Jun 2020 18:15:17 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=6E7C07874D0329E18A0C07E5E303F005; Path=/; HttpOnly
Transfer-Encoding: chunked
Vary: Origin
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

请求标题

Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Authorization: Bearer eyJhbGciOiJIUzINiJ9.eyJyb2xlIjiU0VDVE9SSEVBRCIsImxldmVsRG93biI6IkVENzA0MTI7TU04MzcyNDtKTDgzNTwO0RNNDAwNzE7Skc3MzA0NjtFQzM0NjEzO05OMTY5Nzk7QUs2MDYzNztTVDE4NTg4O0FTMjczNTE7Q0I4MTg3OTtWQTc4MTk5O0NNOTM3MDA7QVkyMzYzNztKUzcwMDY4O0NCMTc2NzE7TksyMTU2MDtMUzg4OTg0O0FQNTg3MDg7VFcyjk0NTtKSzI1Nzc3O01TNDk5MjE7SkI4OTcyOTtNSDAyMTI3O01CMTUwODk7SU0xMjgwODtNQzcxOTc2O1JSMjAzMDI7TFM1ODk4MiIsImxldmVsVXAiOm51bGwsImRlbGVnYXRlZCI6bnVsbCwic29lSWQiOiJTUjQ0MTg1I0.*************
Cache-Control: no-cache
Connection: keep-alive
Content-type: application/json
Host: localhost:9081
Origin: http://localhost:7000
Pragma: no-cache
Referer: http://localhost:7000/build/standalone.html
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36

我在响应标头中未获得以下预期标头

Content-Encoding: gzip
Vary: Accept-Encoding

客户端/服务器端是否需要任何更改?
---[编辑-1] -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
以下是从浏览器调用rest api时的响应标头

Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Sun, 28 Jun 2020 18:12:29 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding

-----[编辑-2]---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用ziplet依赖项我可以压缩响应,但我想使用 Spring Boot gzip压缩。
响应标头-使用Ziplet时

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:7000
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Encoding: gzip
Content-Type: application/json;charset=UTF-8
Date: Mon, 06 Jul 2020 18:31:07 GMT
Expires: 0
Pragma: no-cache
Set-Cookie: JSESSIONID=8465D2E81A1A9CE146255B6C545FBE30; Path=/; HttpOnly
Transfer-Encoding: chunked
Vary: Accept-Encoding
Vary: Origin
X-Application-Context: application
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

使用ziplet时-我可以看到***压缩过滤器:压缩筛选器已初始化***

当使用Sping Boot gzip压缩时,没有观察到任何我可以假设gzip压缩已启用的地方。
有任何属性可以调试嵌入式tomcat的spring Boot gzip压缩吗?比如logging.level.org.eclipse.jetty.server.handler.gzip=TRACE
如何验证服务器压缩是否已启用?
在我的项目中,客户端/服务器端是否需要任何更改?
先谢谢你。

0h4hbjxa

0h4hbjxa1#

正如您的问题,您使用的是Sping Boot 1.5.10.RELEASE。
对于该版本的框架,类TomcatEmbeddedServletContainerFactory负责启动嵌入式Tomcat容器。
该类和版本的源代码可在此处找到:
https://github.com/spring-projects/spring-boot/blob/v1.5.10.RELEASE/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.java
在这个类中,可以找到customizeCompression方法:

private void customizeCompression(Connector connector) {
  ProtocolHandler handler = connector.getProtocolHandler();
  if (handler instanceof AbstractHttp11Protocol) {
    AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
    Compression compression = getCompression();
    protocol.setCompression("on");
    protocol.setCompressionMinSize(compression.getMinResponseSize());
    configureCompressibleMimeTypes(protocol, compression);
    if (getCompression().getExcludedUserAgents() != null) {
      protocol.setNoCompressionUserAgents(
          StringUtils.arrayToCommaDelimitedString(
              getCompression().getExcludedUserAgents()));
    }
  }
}

请尝试在此方法上设置断点并调试应用程序,以查看是否实际启用了压缩。
如果没有,则很可能是项目中存在某种错误的配置。
如果它工作正常,则表明您的配置是正确的,问题是不同的。
如果是这种情况,在查看其他服务器之前,尝试另一个服务器(如Jetty或Undertow)可能是合适的,这些服务器也支持此压缩功能,并查看那里是否一切正常。
例如,要配置Undertow而不是Tomcat,可以使用以下命令:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

您也可以尝试以编程方式配置Tomcat压缩;请参阅matsev在以下stackoverflow问题中回答:
Using GZIP compression with Spring Boot/MVC/JavaConfig with RESTful

mklgxw1f

mklgxw1f2#

您必须在 Spring Boot 属性中启用http2支持

server.http2.enabled=true

然后尝试

相关问题