Skip to content

Commit 5559969

Browse files
committed
Enable Tomcat to use HTTP/2
1 parent ac4efa0 commit 5559969

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DefaultServletWebServerFactoryCustomizer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.coyote.AbstractProtocol;
3030
import org.apache.coyote.ProtocolHandler;
3131
import org.apache.coyote.http11.AbstractHttp11Protocol;
32+
import org.apache.coyote.http2.Http2Protocol;
3233
import org.eclipse.jetty.server.AbstractConnector;
3334
import org.eclipse.jetty.server.ConnectionFactory;
3435
import org.eclipse.jetty.server.Handler;
@@ -46,6 +47,7 @@
4647
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
4748
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
4849
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
50+
import org.springframework.boot.web.server.Http2;
4951
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
5052
import org.springframework.boot.web.servlet.ServletContextInitializer;
5153
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
@@ -213,6 +215,8 @@ private static class TomcatCustomizer {
213215
public static void customizeTomcat(ServerProperties serverProperties,
214216
Environment environment, TomcatServletWebServerFactory factory) {
215217

218+
customizeTomcatHttp2(factory, serverProperties);
219+
216220
ServerProperties.Tomcat tomcatProperties = serverProperties.getTomcat();
217221
if (tomcatProperties.getBasedir() != null) {
218222
factory.setBaseDirectory(tomcatProperties.getBasedir());
@@ -261,6 +265,20 @@ public static void customizeTomcat(ServerProperties serverProperties,
261265
}
262266
}
263267

268+
private static void customizeTomcatHttp2(TomcatServletWebServerFactory factory,
269+
ServerProperties serverProperties) {
270+
271+
Http2 http2Properties = serverProperties.getHttp2();
272+
273+
if (http2Properties.isEnabled()) {
274+
enableTomcatHttp2(factory);
275+
}
276+
}
277+
278+
private static void enableTomcatHttp2(TomcatServletWebServerFactory factory) {
279+
factory.addConnectorCustomizers(connector -> connector.addUpgradeProtocol(new Http2Protocol()));
280+
}
281+
264282
private static void customizeAcceptCount(TomcatServletWebServerFactory factory,
265283
final int acceptCount) {
266284
factory.addConnectorCustomizers((connector) -> {

0 commit comments

Comments
 (0)