SpringBoot2, enables http2 cleartext

Http2 has many advantages over http 1.0/1.1

However, Springboot doc says it does only support h2 (http2 over SSL/TLS) not h2c (http2 over cleartext). If it is a browser / server application that’s reasonable, because browsers only support h2. The problem is, today, if we use springboot to create a micro-service application, the app usually runs behind a firewall / loadbalancer, in a trusted zone. We do not want SSL/TLS enabled to save the resouces (CPU when decrypting / encrypting, and labor on managing the SSL/TLS certificates).

Prerequirement:
- JDK 11+ (Not tested with JDK8)
- Springboot 2.3.4

To enabled http2/cleartext, the following code can be used:

A simple restful controller for demo:

@RestController
public class HelloController {
@GetMapping("/")
public String hello() {
return "hello";
}
}

A config class:

Component
public class Config {
@Configuration
@ConditionalOnClass(Undertow.class)
@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
public class
UndertowHttp2Configuration {

@Bean
public ServletWebServerFactory servletWebServerFactory() {
UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
factory.addBuilderCustomizers(builder -> builder.setServerOption(ENABLE_HTTP2, true));
return factory;
}

}

}

And maven pom.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.3.4.RELEASE</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>h2c</artifactId>


<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

</dependencies>

</project>

And the springboot main class (obmitted).

Run the application, test with curl:

for normal http 1.1:

curl -v localhost:8080
* Trying ::1:8080...
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.73.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Connection: keep-alive
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 5
< Date: Thu, 22 Oct 2020 02:39:00 GMT
<
hello* Connection #0 to host localhost left intact

for new http2.0:

curl -v --http2 localhost:8080
* Trying ::1:8080...
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.73.0
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAAQCAAAAAAIAAAAA
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 101 Switching Protocols
< Connection: Upgrade
< Upgrade: h2c
< Date: Thu, 22 Oct 2020 02:39:06 GMT
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=115
* Connection state changed (MAX_CONCURRENT_STREAMS == 4294967295)!
< HTTP/2 200
< content-type: text/plain;charset=UTF-8
< content-length: 5
< date: Thu, 22 Oct 2020 02:39:06 GMT
<
hello* Connection #0 to host localhost left intact

The upgrade: h2c is what do we expected.

I think springboot documents should be updated to support it.

PS: It seems that the UndertowAutoConfiguration class has no code for setting http2. So it is not possible to enable http2 by application.yaml/bootstrap.yaml or annotations.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store