An open API service indexing awesome lists of open source software.

https://github.com/daggerok/spring-boot-http2

Enable HTTP 2.0 in spring-boot 2 non netty (non webflux) apps running on java 9+
https://github.com/daggerok/spring-boot-http2

http2 spring-boot spring-boot-2 spring-boot-https

Last synced: about 1 year ago
JSON representation

Enable HTTP 2.0 in spring-boot 2 non netty (non webflux) apps running on java 9+

Awesome Lists containing this project

README

          

= spring-boot-http2 image:https://travis-ci.org/daggerok/spring-boot-http2.svg?branch=master["Build Status", link="https://travis-ci.org/daggerok/spring-boot-http2"]

////
image:https://gitlab.com/daggerok/spring-boot-http2/badges/master/build.svg["Build Status", link="https://gitlab.com/daggerok/spring-boot-http2/-/jobs"]
image:https://img.shields.io/bitbucket/pipelines/daggerok/spring-boot-http2.svg["Build Status", link="https://bitbucket.com/daggerok/spring-boot-http2"]
////

//tag::content[]

== HTTP 2.0 with spring-boot 2 on java 9+

read link:https://daggerok.github.io/spring-boot-http2[project reference documentation]

=== TL;DR

Example how to enable HTTP 2.0 in spring-boot 2 non netty (non webflux) apps running on java 9+

=== certificate

.generate self signed sertificates
[source,bash]
----
# server side:
keytool -genkeypair -alias my-keystore -keyalg RSA -keystore src/main/resources/my-keystore.jks \
-dname "CN=boot-jwt, L=Odessa, S=Odessa, C=UA" \
-keypass my-keystore-password -storepass my-keystore-password

keytool -list -rfc --keystore src/main/resources/my-keystore.jks | openssl x509 -inform pem -pubkey > src/main/resources/my-keystore.pem
Enter keystore password: my-keystore-password

# client side:
openssl x509 -pubkey -noout -in src/main/resources/my-keystore.pem > src/main/resources/my-keystore-pubkey.pem
----

////
.output
[source,bash]
----
Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore src/main/resources/my-keystore.jks -destkeystore src/main/resources/my-keystore.jks -deststoretype pkcs12".
----

.so let's follow recommendations
[source,bash]
----
keytool -importkeystore -srckeystore src/main/resources/my-keystore.jks -destkeystore src/main/resources/my-keystore.jks -deststoretype pkcs12
----

.output
[source,bash]
----
Enter source keystore password: # enter here my-keystore-password
Entry for alias my-keystore successfully imported.
Import command completed: 1 entries successfully imported, 0 entries failed or cancelled

Warning:
Migrated "src/main/resources/my-keystore.jks" to Non JKS/JCEKS. The JKS keystore is backed up as "src/main/resources/my-keystore.jks.old".
----
////

so now we have it:
- server will consume file: `src/main/resources/my-keystore.jks`
- clients needs file: `src/main/resources/my-keystore-pubkey.pem`

=== setup spring-boot app

.output
[source,bash]
----
include::./src/main/resources/application.yaml[tags=content]
----

=== install java 9 or 10 or use jenv to switch java version

.http 2.0 is working on java 9+, so lets switch on it:
[source,bash]
----
# install jenv to easily switch between java verions:
brew install jenv

# install java 9

# all installed JVMs on my mac:
ls -l /Library/Java/JavaVirtualMachines/
total 0
drwxr-xr-x 3 root wheel 96 Jul 15 2015 1.6.0.jdk
drwxr-xr-x 3 root wheel 96 Apr 20 22:42 jdk-10.0.1.jdk
drwxr-xr-x 2 root wheel 64 Apr 20 22:42 jdk-10.jdk
drwxr-xr-x 3 root wheel 96 Nov 8 2017 jdk-9.0.1.jdk
drwxr-xr-x 2 root wheel 64 Mar 24 04:08 jdk-9.0.4.jdk
drwxr-xr-x 3 root wheel 96 Mar 28 00:34 jdk1.7.0_80.jdk
drwxr-xr-x 3 root wheel 96 Nov 11 2017 jdk1.8.0_152.jdk
drwxr-xr-x 3 root wheel 96 Mar 19 01:59 jdk1.8.0_161.jdk
drwxr-xr-x 2 root wheel 64 Apr 20 22:44 jdk1.8.0_162.jdk
drwxr-xr-x 3 root wheel 96 Apr 20 22:44 jdk1.8.0_172.jdk

# add JVM 9 to my jenv:
jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/
oracle64-9.0.1 added
9.0 added

# check current JVM:
java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)

# check avalible JVMs to switch to:
jenv versions
system
1.6
1.6.0.65
1.7
1.7.0.80
1.8
1.8.0.152
10.0
10.0.1
9.0
9.0.1
oracle64-1.6.0.65
oracle64-1.7.0.80
* oracle64-1.8.0.152 (set by /private/tmp/spring-boot-http2/.java-version)
oracle64-10.0.1
oracle64-9.0.1

# switch local jvm to JDK 9:
jenv local 9.0.1

# versify:
java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
----

.now build and run project using gradle:
[source,bash]
----
./gradlew clean build
bash ./build/libs/*.jar
----

.switch to java 10 and build using maven:
[source,bash]
----
jenv local 10.0

java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

./mvnw

bash target/*.jar
----

Lastly open browser https://127.0.0.1:8443/ and developer tool and refresh page.
Go to network tab and enable protocol column.
Refresh page and if you can see: `h2` protocol, then everything is correct

generated by link:https://github.com/daggerok/generator-jvm/[generator-jvm] yeoman generator (java-spring-boot)
//end::content[]