Skip to content

Commit 9478cd2

Browse files
committed
Document how to configure h2c protocol
Prior to this commit, the how-to documentation would say that Spring Boot does not support the h2c protocol. While it's not supported out-of-the-box with a configuration property, this protocol can still be configured using server customizers. This commit documents, with code snippets, the server customizers one should use to configure the h2c protocol in an application - for each supported server. Closes gh-21997
1 parent 5eb1e26 commit 9478cd2

File tree

1 file changed

+75
-16
lines changed
  • spring-boot-project/spring-boot-docs/src/main/asciidoc

1 file changed

+75
-16
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -582,25 +582,12 @@ This support depends on the chosen web server and the application environment, s
582582

583583
[NOTE]
584584
====
585-
Spring Boot does not support `h2c`, the cleartext version of the HTTP/2 protocol.
586-
So you must <<howto-configure-ssl, configure SSL first>>.
585+
Spring Boot does not advise using `h2c`, the cleartext version of the HTTP/2 protocol.
586+
As a result, the next sections require to <<howto-configure-ssl, configure SSL first>>.
587+
If you still choose to use `h2c`, you can check <<howto-configure-http2-h2c, the dedicated section>>.
587588
====
588589

589590

590-
591-
[[howto-configure-http2-undertow]]
592-
==== HTTP/2 with Undertow
593-
As of Undertow 1.4.0+, HTTP/2 is supported without any additional requirement on JDK8.
594-
595-
596-
597-
[[howto-configure-http2-jetty]]
598-
==== HTTP/2 with Jetty
599-
As of Jetty 9.4.8, HTTP/2 is also supported with the https://www.conscrypt.org/[Conscrypt library].
600-
To enable that support, your application needs to have two additional dependencies: `org.eclipse.jetty:jetty-alpn-conscrypt-server` and `org.eclipse.jetty.http2:http2-server`.
601-
602-
603-
604591
[[howto-configure-http2-tomcat]]
605592
==== HTTP/2 with Tomcat
606593
Spring Boot ships by default with Tomcat 9.0.x which supports HTTP/2 out of the box when using JDK 9 or later.
@@ -621,6 +608,13 @@ This error is not fatal, and the application still starts with HTTP/1.1 SSL supp
621608

622609

623610

611+
[[howto-configure-http2-jetty]]
612+
==== HTTP/2 with Jetty
613+
As of Jetty 9.4.8, HTTP/2 is also supported with the https://www.conscrypt.org/[Conscrypt library].
614+
To enable that support, your application needs to have two additional dependencies: `org.eclipse.jetty:jetty-alpn-conscrypt-server` and `org.eclipse.jetty.http2:http2-server`.
615+
616+
617+
624618
[[howto-configure-http2-netty]]
625619
==== HTTP/2 with Reactor Netty
626620
The `spring-boot-webflux-starter` is using by default Reactor Netty as a server.
@@ -633,6 +627,71 @@ Developers can choose to import only the required dependencies using a classifie
633627

634628

635629

630+
[[howto-configure-http2-undertow]]
631+
==== HTTP/2 with Undertow
632+
As of Undertow 1.4.0+, HTTP/2 is supported without any additional requirement on JDK8.
633+
634+
635+
636+
[[howto-configure-http2-h2c]]
637+
==== h2c with supported servers
638+
To enable `h2c`, you need to leave the configprop:server.http2.enabled[] property set to `false`,
639+
and instead apply a customizer specific to your choice of server:
640+
641+
For Tomcat, we need to add an upgrade protocol:
642+
643+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
644+
----
645+
@Bean
646+
public TomcatConnectorCustomizer connectorCustomizer() {
647+
return (connector) -> connector.addUpgradeProtocol(new Http2Protocol());
648+
}
649+
----
650+
651+
For Jetty, we need to add a connection factory to the existing connector:
652+
653+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
654+
----
655+
@Bean
656+
public JettyServerCustomizer serverCustomizer() {
657+
return (server) -> {
658+
HttpConfiguration configuration = new HttpConfiguration();
659+
configuration.setSendServerVersion(false);
660+
Arrays.stream(server.getConnectors())
661+
.filter(connector -> connector instanceof ServerConnector)
662+
.map(ServerConnector.class::cast)
663+
.forEach(connector -> {
664+
connector.addConnectionFactory(new HTTP2CServerConnectionFactory(configuration));
665+
});
666+
};
667+
}
668+
----
669+
670+
For Netty, we need to add `h2c` as a supported protocol:
671+
672+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
673+
----
674+
@Bean
675+
public NettyServerCustomizer serverCustomizer() {
676+
return (server) -> server.protocol(HttpProtocol.H2C);
677+
}
678+
----
679+
680+
For Undertow, we need to enable the HTTP2 option:
681+
682+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
683+
----
684+
@Bean
685+
public UndertowBuilderCustomizer builderCustomizer() {
686+
return (builder) -> {
687+
builder.setServerOption(ENABLE_HTTP2, true);
688+
};
689+
}
690+
----
691+
692+
693+
694+
636695
[[howto-configure-webserver]]
637696
=== Configure the Web Server
638697
Generally, you should first consider using one of the many available configuration keys and customize your web server by adding new entries in your `application.properties` (or `application.yml`, or environment, etc. see "`<<howto-discover-build-in-options-for-external-properties>>`").

0 commit comments

Comments
 (0)