即使使用Etag,最大年龄:0,必须重新验证且没有缓存,更改后的静态文件的URL仍返回304

时间:2019-05-10 08:20:23

标签: spring-boot http http-headers browser-cache cache-control

我正尝试在我的Spring Boot应用程序中实现Etag功能,以便浏览器可以在更改服务器映像时更新映像缓存。

我通过YAML文件更改了Spring Boot设置:

spring:
  jackson:
    serialization:
      WRITE_DATES_AS_TIMESTAMPS: false
  resources:
    static-locations: classpath:static/
    chain:
      strategy:
        content:
          enabled: true
    cache:
      cachecontrol:
        max-age: 0
        must-revalidate: true
        no-cache: true
  mvc:
    static-path-pattern: /resources/**

并添加了ShallowEtagHeaderFilter bean:

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Bean
    public ShallowEtagHeaderFilter shallowEtagHeaderFilter() {
        return new ShallowEtagHeaderFilter();
    }
}

第一个请求和响应标头:

GET /resources/image/product/list/foo.png HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
DNT: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7,vi;q=0.6
Cookie: _ga=GA1.1.134751806.1551948465; loginFlag=undefined; branchId=admin

HTTP/1.1 200
Last-Modified: Wed, 08 May 2019 05:17:32 GMT
Cache-Control: no-cache, must-revalidate
Accept-Ranges: bytes
ETag: "0b99ccc3070d4e65cd155b768a56f97e4"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Content-Type: image/png
Content-Length: 101070
Date: Fri, 10 May 2019 08:03:37 GMT

第二个请求和响应标头,而不更改静态文件:

GET /resources/image/product/list/foo.png HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
DNT: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7,vi;q=0.6
Cookie: _ga=GA1.1.134751806.1551948465; loginFlag=undefined; branchId=admin
If-None-Match: "0b99ccc3070d4e65cd155b768a56f97e4"
If-Modified-Since: Wed, 08 May 2019 05:17:32 GMT

HTTP/1.1 304
Last-Modified: Wed, 08 May 2019 05:17:32 GMT
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Date: Fri, 10 May 2019 08:05:32 GMT

第三,我停止了本地主机服务器,用其他文件覆盖了foo.png,重建了项目并运行了服务器。我希望请求应返回更改后的图像文件,但是状态为304,图像与以前相同:

GET /resources/image/product/list/foo.png HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36
DNT: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7,vi;q=0.6
Cookie: _ga=GA1.1.134751806.1551948465; loginFlag=undefined; branchId=admin
If-None-Match: "0b99ccc3070d4e65cd155b768a56f97e4"
If-Modified-Since: Wed, 08 May 2019 05:17:32 GMT

HTTP/1.1 304
Last-Modified: Sun, 05 May 2019 19:08:52 GMT
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Frame-Options: DENY
Date: Fri, 10 May 2019 08:09:32 GMT

我不知道如何在更改文件时刷新浏览器的缓存。我想念什么?

1 个答案:

答案 0 :(得分:0)

似乎设置没有问题。我没有通过IDE(IntelliJ IDEA)构建和运行项目,而是尝试了Gradle bootJar并运行了jar文件,它可以按预期工作。