Add option for ignoring last-modified for static resources

Prior to this commit, the resource handler serving static resources for
Spring MVC and Spring WebFlux would always look at the
`Resource#lastModified` information, derive the `"Last-Modified"` HTTP
response header and support HTTP conditional requests with that
information.

In some cases, builds or packaging tools choose to set this last
modification date to a static date in the past. This allows tools to
have reproducible builds or to leverage caching given the static
resources content didn't change.

This can lead to problems where this static date (e.g. "1980-01-01") is
used literally in HTTP responses and will make the HTTP caching
mechanism counter-productive: the content of the resources changed, but
the application insists on saying it didn't change since the 80s...

This commit adds a new configuration option to disable this support -
there is no way to automatically discard those dates: there is no
standard for that and many don't use he "EPOCH 0 date" as it can lead to
compatibility issues with different OSes.

Closes gh-25845
This commit is contained in:
Brian Clozel
2020-10-06 16:23:51 +02:00
parent cf61545f41
commit a0af552d0f
9 changed files with 124 additions and 8 deletions

View File

@@ -5621,8 +5621,8 @@ In the next example, given a request that starts with `/resources`, the relative
used to find and serve static resources relative to `/public` under the web application
root or on the classpath under `/static`. The resources are served with a one-year future
expiration to ensure maximum use of the browser cache and a reduction in HTTP requests
made by the browser. The `Last-Modified` header is also evaluated and, if present, a `304`
status code is returned.
made by the browser. The `Last-Modified` information is deduced from `Resource#lastModified`
so that HTTP conditional requests are supported with `"Last-Modified"` headers.
The following listing shows how to do so with Java configuration:
@@ -5637,7 +5637,7 @@ The following listing shows how to do so with Java configuration:
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCachePeriod(31556926);
.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)));
}
}
----
@@ -5651,7 +5651,7 @@ The following listing shows how to do so with Java configuration:
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCachePeriod(31556926)
.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)))
}
}
----