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:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -238,6 +238,13 @@ public class ResourceHandlerRegistryTests {
|
||||
assertThat(locationCharsets.values().iterator().next()).isEqualTo(StandardCharsets.ISO_8859_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void lastModifiedDisabled() {
|
||||
this.registration.setUseLastModified(false);
|
||||
ResourceHttpRequestHandler handler = getHandler("/resources/**");
|
||||
assertThat(handler.isUseLastModified()).isFalse();
|
||||
}
|
||||
|
||||
private ResourceHttpRequestHandler getHandler(String pathPattern) {
|
||||
SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
|
||||
return (ResourceHttpRequestHandler) hm.getUrlMap().get(pathPattern);
|
||||
|
||||
@@ -667,6 +667,18 @@ public class ResourceHttpRequestHandlerTests {
|
||||
assertThat(this.response.getHeader("Cache-Control")).isEqualTo("max-age=3600");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreLastModified() throws Exception {
|
||||
this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.css");
|
||||
this.handler.setUseLastModified(false);
|
||||
this.handler.handleRequest(this.request, this.response);
|
||||
|
||||
assertThat(this.response.getContentType()).isEqualTo("text/css");
|
||||
assertThat(this.response.getContentLength()).isEqualTo(17);
|
||||
assertThat(this.response.containsHeader("Last-Modified")).isFalse();
|
||||
assertThat(this.response.getContentAsString()).isEqualTo("h1 { color:red; }");
|
||||
}
|
||||
|
||||
|
||||
private long resourceLastModified(String resourceName) throws IOException {
|
||||
return new ClassPathResource(resourceName, getClass()).getFile().lastModified();
|
||||
|
||||
Reference in New Issue
Block a user