Skip to content

Commit 0bfedb3

Browse files
committed
Merge pull request #13614 from chtompki:issue-13613
* pr/13614: Polish "Add configuration for Tomcat's cachingAllowed property" Add configuration for Tomcat's cachingAllowed property
2 parents 101afc9 + 5fb2060 commit 0bfedb3

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,24 @@ public void setBuffered(boolean buffered) {
660660
*/
661661
public static class Resource {
662662

663+
/**
664+
* Whether static resource caching is permitted for this web application.
665+
*/
666+
private boolean allowCaching = true;
667+
663668
/**
664669
* Time-to-live of the static resource cache.
665670
*/
666671
private Duration cacheTtl;
667672

673+
public boolean isAllowCaching() {
674+
return this.allowCaching;
675+
}
676+
677+
public void setAllowCaching(boolean allowCaching) {
678+
this.allowCaching = allowCaching;
679+
}
680+
668681
public Duration getCacheTtl() {
669682
return this.cacheTtl;
670683
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
241241
private void customizeStaticResources(ConfigurableTomcatWebServerFactory factory) {
242242
ServerProperties.Tomcat.Resource resource = this.serverProperties.getTomcat()
243243
.getResource();
244-
if (resource.getCacheTtl() == null) {
245-
return;
246-
}
247244
factory.addContextCustomizers((context) -> {
248245
context.addLifecycleListener((event) -> {
249246
if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
250-
long ttl = resource.getCacheTtl().toMillis();
251-
context.getResources().setCacheTtl(ttl);
247+
context.getResources().setCachingAllowed(resource.isAllowCaching());
248+
if (resource.getCacheTtl() != null) {
249+
long ttl = resource.getCacheTtl().toMillis();
250+
context.getResources().setCacheTtl(ttl);
251+
}
252252
}
253253
});
254254
});

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616

1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

19+
import java.util.Map;
1920
import java.util.function.Consumer;
2021

2122
import org.apache.catalina.Context;
2223
import org.apache.catalina.Valve;
24+
import org.apache.catalina.mapper.Mapper;
2325
import org.apache.catalina.startup.Tomcat;
2426
import org.apache.catalina.valves.AccessLogValve;
2527
import org.apache.catalina.valves.ErrorReportValve;
2628
import org.apache.catalina.valves.RemoteIpValve;
29+
import org.apache.catalina.webresources.StandardRoot;
2730
import org.apache.coyote.AbstractProtocol;
2831
import org.junit.Before;
2932
import org.junit.Test;
@@ -36,6 +39,7 @@
3639
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
3740
import org.springframework.mock.env.MockEnvironment;
3841
import org.springframework.test.context.support.TestPropertySourceUtils;
42+
import org.springframework.test.util.ReflectionTestUtils;
3943

4044
import static org.assertj.core.api.Assertions.assertThat;
4145

@@ -44,6 +48,7 @@
4448
*
4549
* @author Brian Clozel
4650
* @author Phillip Webb
51+
* @author Rob Tompkins
4752
*/
4853
public class TomcatWebServerFactoryCustomizerTests {
4954

@@ -121,6 +126,20 @@ public void customRemoteIpValve() {
121126
assertThat(remoteIpValve.getInternalProxies()).isEqualTo("192.168.0.1");
122127
}
123128

129+
@Test
130+
public void customStaticResourceAllowCaching() {
131+
bind("server.tomcat.resource.allow-caching=false");
132+
customizeAndRunServer((server) -> {
133+
Mapper mapper = server.getTomcat().getService().getMapper();
134+
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
135+
"contextObjectToContextVersionMap");
136+
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
137+
.values().toArray()[0];
138+
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
139+
"resources")).isCachingAllowed()).isFalse();
140+
});
141+
}
142+
124143
@Test
125144
public void customStaticResourceCacheTtl() {
126145
bind("server.tomcat.resource.cache-ttl=10000");

spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ content into your application. Rather, pick only the properties that you need.
259259
server.tomcat.protocol-header-https-value=https # Value of the protocol header indicating whether the incoming request uses SSL.
260260
server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.
261261
server.tomcat.remote-ip-header= # Name of the HTTP header from which the remote IP is extracted. For instance, `X-FORWARDED-FOR`.
262+
server.tomcat.resource.allow-caching= # Whether static resource caching is permitted for this web application.
262263
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
263264
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
264265
server.tomcat.use-relative-redirects= # Whether HTTP 1.1 and later location headers generated by a call to sendRedirect will use relative or absolute redirects.

0 commit comments

Comments
 (0)