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

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 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.
@@ -21,6 +21,7 @@ import java.util.Arrays;
import java.util.List;
import org.springframework.cache.Cache;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -50,6 +51,8 @@ public class ResourceHandlerRegistration {
@Nullable
private ResourceChainRegistration resourceChainRegistration;
private boolean useLastModified = true;
/**
* Create a {@link ResourceHandlerRegistration} instance.
@@ -109,6 +112,18 @@ public class ResourceHandlerRegistration {
return this;
}
/**
* Set whether the {@link Resource#lastModified()} information should be used to drive HTTP responses.
* <p>This configuration is set to {@code true} by default.
* @param useLastModified whether the "last modified" resource information should be used.
* @return the same {@link ResourceHandlerRegistration} instance, for chained method invocation
* @since 5.3
*/
public ResourceHandlerRegistration setUseLastModified(boolean useLastModified) {
this.useLastModified = useLastModified;
return this;
}
/**
* Configure a chain of resource resolvers and transformers to use. This
* can be useful, for example, to apply a version strategy to resource URLs.
@@ -172,6 +187,7 @@ public class ResourceHandlerRegistration {
else if (this.cachePeriod != null) {
handler.setCacheSeconds(this.cachePeriod);
}
handler.setUseLastModified(this.useLastModified);
return handler;
}

View File

@@ -140,6 +140,8 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
@Nullable
private StringValueResolver embeddedValueResolver;
private boolean useLastModified = true;
public ResourceHttpRequestHandler() {
super(HttpMethod.GET.name(), HttpMethod.HEAD.name());
@@ -346,6 +348,27 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
this.embeddedValueResolver = resolver;
}
/**
* Return whether the {@link Resource#lastModified()} information is used
* to drive HTTP responses when serving static resources.
* @since 5.3
*/
public boolean isUseLastModified() {
return this.useLastModified;
}
/**
* Set whether we should look at the {@link Resource#lastModified()}
* when serving resources and use this information to drive {@code "Last-Modified"}
* HTTP response headers.
* <p>This option is enabled by default and should be turned off if the metadata of
* the static files should be ignored.
* @param useLastModified whether to use the resource last-modified information.
* @since 5.3
*/
public void setUseLastModified(boolean useLastModified) {
this.useLastModified = useLastModified;
}
@Override
public void afterPropertiesSet() throws Exception {
@@ -498,7 +521,7 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
checkRequest(request);
// Header phase
if (new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
if (isUseLastModified() && new ServletWebRequest(request, response).checkNotModified(resource.lastModified())) {
logger.trace("Resource not modified");
return;
}

View File

@@ -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);

View File

@@ -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();