Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
* @author Aurélien Leboulanger
* @author Brian Clozel
* @author Olivier Lamy
* @author Rob Tompkins
*/
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
public class ServerProperties {
Expand Down Expand Up @@ -265,6 +266,11 @@ public static class Tomcat {
*/
private final Accesslog accesslog = new Accesslog();

/**
* Web resource configuration.
*/
private final WebResource webResource = new WebResource();

/**
* Regular expression matching trusted IP addresses.
*/
Expand Down Expand Up @@ -399,6 +405,10 @@ public Accesslog getAccesslog() {
return this.accesslog;
}

public WebResource getWebResource() {
return this.webResource;
}

public Duration getBackgroundProcessorDelay() {
return this.backgroundProcessorDelay;
}
Expand Down Expand Up @@ -515,6 +525,26 @@ public Resource getResource() {
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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import java.time.Duration;

import org.apache.catalina.Lifecycle;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol;
import org.apache.coyote.ProtocolHandler;
import org.apache.coyote.http11.AbstractHttp11Protocol;
Expand All @@ -46,6 +48,7 @@
* @author Yulin Qin
* @author Stephane Nicoll
* @author Phillip Webb
* @author Rob Tompkins
* @since 2.0.0
*/
public class TomcatWebServerFactoryCustomizer implements
Expand All @@ -70,6 +73,8 @@ public int getOrder() {
public void customize(ConfigurableTomcatWebServerFactory factory) {
ServerProperties properties = this.serverProperties;
ServerProperties.Tomcat tomcatProperties = properties.getTomcat();
ServerProperties.Tomcat.WebResource tomcatWebResourceProperties = tomcatProperties
.getWebResource();
PropertyMapper propertyMapper = PropertyMapper.get();
propertyMapper.from(tomcatProperties::getBasedir).whenNonNull()
.to(factory::setBaseDirectory);
Expand Down Expand Up @@ -101,6 +106,9 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
.to((maxConnections) -> customizeMaxConnections(factory, maxConnections));
propertyMapper.from(tomcatProperties::getAcceptCount).when(this::isPositive)
.to((acceptCount) -> customizeAcceptCount(factory, acceptCount));
propertyMapper.from(tomcatWebResourceProperties::getUseCaching).whenFalse()
.to((isWebResourceCachingAllowed) -> customizeWebResourceCaching(factory,
isWebResourceCachingAllowed));
customizeStaticResources(factory);
customizeErrorReportValve(properties.getError(), factory);
}
Expand All @@ -126,6 +134,18 @@ private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory,
});
}

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,
int maxConnections) {
factory.addConnectorCustomizers((connector) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

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

import java.util.Map;
import java.util.function.Consumer;

import org.apache.catalina.Context;
import org.apache.catalina.Valve;
import org.apache.catalina.mapper.Mapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.valves.ErrorReportValve;
import org.apache.catalina.valves.RemoteIpValve;
import org.apache.catalina.webresources.StandardRoot;
import org.apache.coyote.AbstractProtocol;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -36,6 +39,7 @@
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import org.springframework.test.util.ReflectionTestUtils;

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

Expand All @@ -44,6 +48,7 @@
*
* @author Brian Clozel
* @author Phillip Webb
* @author Rob Tompkins
*/
public class TomcatWebServerFactoryCustomizerTests {

Expand Down Expand Up @@ -94,6 +99,20 @@ public void customMaxConnections() {
.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
public void customMaxHttpPostSize() {
bind("server.tomcat.max-http-post-size=10000");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,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.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.webresource.use-caching= # Whether tomcat WebResource caching is permitted for this web application.
server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Whether to enable the access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.
Expand Down