From dfc1979ea2fa205cfca8c530a429ca93603cc76a Mon Sep 17 00:00:00 2001 From: apoplexy Date: Thu, 22 May 2014 11:33:10 +0100 Subject: [PATCH] Allow the tomcat maxHttpHeaderSize to be changed in external config Prevents large SPNEGO headers from causing server errors for example. Added the property to ServerProperties. Fixes gh-931 --- .../autoconfigure/web/ServerProperties.java | 26 ++++++++++++++++++- .../web/ServerPropertiesTests.java | 11 +++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 9d3d9f8f201..e8e7e39a9b0 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -27,6 +27,7 @@ import org.apache.catalina.valves.AccessLogValve; import org.apache.catalina.valves.RemoteIpValve; import org.apache.coyote.AbstractProtocol; import org.apache.coyote.ProtocolHandler; +import org.apache.coyote.http11.AbstractHttp11Protocol; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; @@ -41,7 +42,7 @@ import org.springframework.util.StringUtils; * {@link ConfigurationProperties properties} for a web server (e.g. port and path * settings). Will be used to customize an {@link EmbeddedServletContainerFactory} when an * {@link EmbeddedServletContainerCustomizerBeanPostProcessor} is active. - * + * * @author Dave Syer * @author Stephane Nicoll */ @@ -145,6 +146,8 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer { private int maxThreads = 0; // Number of threads in protocol handler + private int maxHttpHeaderSize = 0; // bytes + private String uriEncoding; public int getMaxThreads() { @@ -155,6 +158,14 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer { this.maxThreads = maxThreads; } + public int getMaxHttpHeaderSize() { + return maxHttpHeaderSize; + } + + public void setMaxHttpHeaderSize(int maxHttpHeaderSize) { + this.maxHttpHeaderSize = maxHttpHeaderSize; + } + public boolean getAccessLogEnabled() { return this.accessLogEnabled; } @@ -246,6 +257,19 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer { }); } + if (this.maxHttpHeaderSize > 0) { + factory.addConnectorCustomizers(new TomcatConnectorCustomizer() { + @Override + public void customize(Connector connector) { + ProtocolHandler handler = connector.getProtocolHandler(); + if (handler instanceof AbstractHttp11Protocol) { + AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler; + protocol.setMaxHttpHeaderSize(Tomcat.this.maxHttpHeaderSize); + } + } + }); + } + if (this.accessLogEnabled) { AccessLogValve valve = new AccessLogValve(); String accessLogPattern = getAccessLogPattern(); diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index 229c7cd21f1..98120e436c8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -34,7 +34,7 @@ import static org.mockito.Mockito.verify; /** * Tests for {@link ServerProperties}. - * + * * @author Dave Syer * @author Stephane Nicoll */ @@ -97,4 +97,13 @@ public class ServerPropertiesTests { assertEquals("US-ASCII", this.properties.getTomcat().getUriEncoding()); } + @Test + public void testCustomizeTomcatHeaderSize() throws Exception { + Map map = new HashMap(); + map.put("server.tomcat.maxHttpHeaderSize", "9999"); + new RelaxedDataBinder(this.properties, "server").bind(new MutablePropertyValues( + map)); + assertEquals(9999, this.properties.getTomcat().getMaxHttpHeaderSize()); + } + }