Commit 200ac6db authored by Rob Tompkins's avatar Rob Tompkins Committed by Stephane Nicoll

Add configuration for Tomcat's cachingAllowed property

See gh-13614
parent 101afc9b
...@@ -52,6 +52,7 @@ import org.springframework.util.StringUtils; ...@@ -52,6 +52,7 @@ import org.springframework.util.StringUtils;
* @author Aurélien Leboulanger * @author Aurélien Leboulanger
* @author Brian Clozel * @author Brian Clozel
* @author Olivier Lamy * @author Olivier Lamy
* @author Rob Tompkins
*/ */
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties { public class ServerProperties {
...@@ -265,6 +266,11 @@ public class ServerProperties { ...@@ -265,6 +266,11 @@ public class ServerProperties {
*/ */
private final Accesslog accesslog = new Accesslog(); private final Accesslog accesslog = new Accesslog();
/**
* Web resource configuration.
*/
private final WebResource webResource = new WebResource();
/** /**
* Regular expression matching trusted IP addresses. * Regular expression matching trusted IP addresses.
*/ */
...@@ -399,6 +405,10 @@ public class ServerProperties { ...@@ -399,6 +405,10 @@ public class ServerProperties {
return this.accesslog; return this.accesslog;
} }
public WebResource getWebResource() {
return this.webResource;
}
public Duration getBackgroundProcessorDelay() { public Duration getBackgroundProcessorDelay() {
return this.backgroundProcessorDelay; return this.backgroundProcessorDelay;
} }
...@@ -515,6 +525,26 @@ public class ServerProperties { ...@@ -515,6 +525,26 @@ public class ServerProperties {
return this.resource; return this.resource;
} }
/**
* Tomcat web resource properties.
*/
public static class WebResource {
/**
* Whether tomcat WebResource caching is permitted for this web application.
*/
private Boolean useCaching = Boolean.TRUE;
public Boolean getUseCaching() {
return this.useCaching;
}
public void setUseCaching(Boolean useCaching) {
this.useCaching = useCaching;
}
}
/** /**
* Tomcat access log properties. * Tomcat access log properties.
*/ */
......
...@@ -19,9 +19,11 @@ package org.springframework.boot.autoconfigure.web.embedded; ...@@ -19,9 +19,11 @@ package org.springframework.boot.autoconfigure.web.embedded;
import java.time.Duration; import java.time.Duration;
import org.apache.catalina.Lifecycle; import org.apache.catalina.Lifecycle;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.valves.AccessLogValve; import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ErrorReportValve; import org.apache.catalina.valves.ErrorReportValve;
import org.apache.catalina.valves.RemoteIpValve; import org.apache.catalina.valves.RemoteIpValve;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler; import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.AbstractHttp11Protocol; import org.apache.coyote.http11.AbstractHttp11Protocol;
...@@ -46,6 +48,7 @@ import org.springframework.util.StringUtils; ...@@ -46,6 +48,7 @@ import org.springframework.util.StringUtils;
* @author Yulin Qin * @author Yulin Qin
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Phillip Webb * @author Phillip Webb
* @author Rob Tompkins
* @since 2.0.0 * @since 2.0.0
*/ */
public class TomcatWebServerFactoryCustomizer implements public class TomcatWebServerFactoryCustomizer implements
...@@ -70,6 +73,8 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -70,6 +73,8 @@ public class TomcatWebServerFactoryCustomizer implements
public void customize(ConfigurableTomcatWebServerFactory factory) { public void customize(ConfigurableTomcatWebServerFactory factory) {
ServerProperties properties = this.serverProperties; ServerProperties properties = this.serverProperties;
ServerProperties.Tomcat tomcatProperties = properties.getTomcat(); ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
ServerProperties.Tomcat.WebResource tomcatWebResourceProperties = tomcatProperties
.getWebResource();
PropertyMapper propertyMapper = PropertyMapper.get(); PropertyMapper propertyMapper = PropertyMapper.get();
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull() propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
.to(factory::setBaseDirectory); .to(factory::setBaseDirectory);
...@@ -101,6 +106,9 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -101,6 +106,9 @@ public class TomcatWebServerFactoryCustomizer implements
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections)); .to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive) propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount)); .to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
propertyMapper.from(tomcatWebResourceProperties::getUseCaching).whenFalse()
.to((isWebResourceCachingAllowed) -> customizeWebResourceCaching(factory,
isWebResourceCachingAllowed));
customizeStaticResources(factory); customizeStaticResources(factory);
customizeErrorReportValve(properties.getError(), factory); customizeErrorReportValve(properties.getError(), factory);
} }
...@@ -126,6 +134,18 @@ public class TomcatWebServerFactoryCustomizer implements ...@@ -126,6 +134,18 @@ public class TomcatWebServerFactoryCustomizer implements
}); });
} }
private void customizeWebResourceCaching(ConfigurableTomcatWebServerFactory factory,
Boolean useWebResourceCaching) {
factory.addContextCustomizers((context) -> {
WebResourceRoot webResourceRoot = context.getResources();
if (webResourceRoot == null) {
webResourceRoot = new StandardRoot(context);
}
webResourceRoot.setCachingAllowed(useWebResourceCaching);
context.setResources(webResourceRoot);
});
}
private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory, private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory,
int maxConnections) { int maxConnections) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
......
...@@ -16,14 +16,17 @@ ...@@ -16,14 +16,17 @@
package org.springframework.boot.autoconfigure.web.embedded; package org.springframework.boot.autoconfigure.web.embedded;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.apache.catalina.Context; import org.apache.catalina.Context;
import org.apache.catalina.Valve; import org.apache.catalina.Valve;
import org.apache.catalina.mapper.Mapper;
import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.AccessLogValve; import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ErrorReportValve; import org.apache.catalina.valves.ErrorReportValve;
import org.apache.catalina.valves.RemoteIpValve; import org.apache.catalina.valves.RemoteIpValve;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol; import org.apache.coyote.AbstractProtocol;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -36,6 +39,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor ...@@ -36,6 +39,7 @@ import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactor
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.mock.env.MockEnvironment; import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils; import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
...@@ -44,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -44,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Brian Clozel * @author Brian Clozel
* @author Phillip Webb * @author Phillip Webb
* @author Rob Tompkins
*/ */
public class TomcatWebServerFactoryCustomizerTests { public class TomcatWebServerFactoryCustomizerTests {
...@@ -94,6 +99,20 @@ public class TomcatWebServerFactoryCustomizerTests { ...@@ -94,6 +99,20 @@ public class TomcatWebServerFactoryCustomizerTests {
.isEqualTo(5)); .isEqualTo(5));
} }
@Test
public void turnOffWebResourceCaching() {
bind("server.tomcat.webresource.use-caching=false");
customizeAndRunServer((server) -> {
Mapper mapper = server.getTomcat().getService().getMapper();
Object contextObjectToContextVersionMap = ReflectionTestUtils.getField(mapper,
"contextObjectToContextVersionMap");
Object tomcatEmbeddedContext = ((Map<Context, Object>) contextObjectToContextVersionMap)
.values().toArray()[0];
assertThat(((StandardRoot) ReflectionTestUtils.getField(tomcatEmbeddedContext,
"resources")).isCachingAllowed()).isFalse();
});
}
@Test @Test
public void customMaxHttpPostSize() { public void customMaxHttpPostSize() {
bind("server.tomcat.max-http-post-size=10000"); bind("server.tomcat.max-http-post-size=10000");
......
...@@ -262,6 +262,7 @@ content into your application. Rather, pick only the properties that you need. ...@@ -262,6 +262,7 @@ content into your application. Rather, pick only the properties that you need.
server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache. server.tomcat.resource.cache-ttl= # Time-to-live of the static resource cache.
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI. server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
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. 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.
server.tomcat.webresource.use-caching= # Whether tomcat WebResource caching is permitted for this web application.
server.undertow.accesslog.dir= # Undertow access log directory. server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Whether to enable the access log. server.undertow.accesslog.enabled=false # Whether to enable the access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs. server.undertow.accesslog.pattern=common # Format pattern for access logs.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment