From 58db841c8f36fbdb4d9b3bf8a2ac6386b0507f65 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 31 Oct 2017 11:50:33 +0100 Subject: [PATCH] Add Http2 configuration properties This commit adds a new configuration properties class for configuring HTTP/2 protocol support. By default, this protocol is disabled as enabling it requires several manual changes: * configuring a web server for proper TLS and ALPN support * configuring a proper SSL certificate See gh-10043 --- .../autoconfigure/web/ServerProperties.java | 8 ++++ .../DefaultReactiveWebServerCustomizer.java | 3 ++ ...aultServletWebServerFactoryCustomizer.java | 3 ++ .../appendix-application-properties.adoc | 1 + .../AbstractConfigurableWebServerFactory.java | 11 ++++++ .../server/ConfigurableWebServerFactory.java | 6 +++ .../boot/web/server/Http2.java | 39 +++++++++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/Http2.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 4c1acd0954e..f5bbccd4a9c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -31,6 +31,7 @@ import java.util.TimeZone; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; import org.springframework.boot.web.server.Compression; +import org.springframework.boot.web.server.Http2; import org.springframework.boot.web.server.Ssl; import org.springframework.boot.web.servlet.server.Jsp; import org.springframework.util.Assert; @@ -102,6 +103,9 @@ public class ServerProperties { @NestedConfigurationProperty private Compression compression = new Compression(); + @NestedConfigurationProperty + private Http2 http2 = new Http2(); + private Servlet servlet = new Servlet(); private final Tomcat tomcat = new Tomcat(); @@ -190,6 +194,10 @@ public class ServerProperties { return this.compression; } + public Http2 getHttp2() { + return this.http2; + } + public Servlet getServlet() { return this.servlet; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerCustomizer.java index 16301bf61f9..db98b056524 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/DefaultReactiveWebServerCustomizer.java @@ -55,6 +55,9 @@ public class DefaultReactiveWebServerCustomizer implements if (this.serverProperties.getCompression() != null) { server.setCompression(this.serverProperties.getCompression()); } + if (this.serverProperties.getHttp2() != null) { + server.setHttp2(this.serverProperties.getHttp2()); + } } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java index c557786755b..313ceab6d5f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java @@ -120,6 +120,9 @@ public class DefaultServletWebServerFactoryCustomizer if (this.serverProperties.getCompression() != null) { factory.setCompression(this.serverProperties.getCompression()); } + if (this.serverProperties.getHttp2() != null) { + factory.setHttp2(this.serverProperties.getHttp2()); + } factory.setServerHeader(this.serverProperties.getServerHeader()); if (factory instanceof TomcatServletWebServerFactory) { TomcatCustomizer.customizeTomcat(this.serverProperties, this.environment, diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ed59b22ed89..bbdfe67a6ab 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -165,6 +165,7 @@ content into your application; rather pick only the properties that you need. server.error.include-stacktrace=never # When to include a "stacktrace" attribute. server.error.path=/error # Path of the error controller. server.error.whitelabel.enabled=true # Enable the default error page displayed in browsers in case of a server error. + server.http2.enabled=true # Enable HTTP/2 support if the current environment supports it. server.jetty.acceptors= # Number of acceptor threads to use. server.jetty.accesslog.append=false # Append to log. server.jetty.accesslog.date-format=dd/MMM/yyyy:HH:mm:ss Z # Timestamp format of the request log. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java index 85ff5e5576a..3d967b0cbc4 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/AbstractConfigurableWebServerFactory.java @@ -50,6 +50,8 @@ public class AbstractConfigurableWebServerFactory private SslStoreProvider sslStoreProvider; + private Http2 http2; + private Compression compression; private String serverHeader; @@ -134,6 +136,15 @@ public class AbstractConfigurableWebServerFactory this.sslStoreProvider = sslStoreProvider; } + public Http2 getHttp2() { + return this.http2; + } + + @Override + public void setHttp2(Http2 http2) { + this.http2 = http2; + } + public Compression getCompression() { return this.compression; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java index 345441d0e83..bbbbbd01d30 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/ConfigurableWebServerFactory.java @@ -62,6 +62,12 @@ public interface ConfigurableWebServerFactory */ void setSslStoreProvider(SslStoreProvider sslStoreProvider); + /** + * Sets the HTTP/2 configuration that will be applied to the server. + * @param http2 the HTTP/2 configuration + */ + void setHttp2(Http2 http2); + /** * Sets the compression configuration that will be applied to the server's default * connector. diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/Http2.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/Http2.java new file mode 100644 index 00000000000..eb3c0e75b54 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/server/Http2.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.web.server; + +/** + * Simple server-independent abstraction for HTTP/2 configuration. + * + * @author Brian Clozel + * @since 2.0.0 + */ +public class Http2 { + + /** + * If HTTP/2 protocol is enabled. + */ + private boolean enabled = false; + + public boolean getEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +}