In this demo, I’ll show how to create a secure REST API and native image with Spring Boot. You’ll see how to run a secure, OAuth 2.0-protected, Java REST API that allows JWT authentication. Then, I’ll compare its performance with Micronaut, Quarkus, and Helidon.
Check this video’s description below for links to its blog post, comments, demo script, and code example.
Prerequisites:
-
SDKMAN (for Java 17 with GraalVM)
-
HTTPie (a better version of cURL)
-
An Okta Developer Account (or the Okta CLI)
Tip
|
The brackets at the end of some steps indicate the IntelliJ Live Templates to use. You can find the template definitions at mraible/idea-live-templates. |
-
Install the Okta CLI and run
okta register
to sign up for a new account. If you already have an account, runokta login
. -
Run
okta apps create spa
. Setoidcdebugger
as an app name and press Enter. -
Use
https://oidcdebugger.com/debug
for the Redirect URI and set the Logout Redirect URI tohttps://oidcdebugger.com
. -
Navigate to the OpenID Connect Debugger website.
-
Fill in your client ID
-
Use
https://{yourOktaDomain}/oauth2/default/v1/authorize
for the Authorize URI -
Select code for the response type and Use PKCE
-
Click Send Request to continue
-
-
Set the access token as a
TOKEN
environment variable in a terminal window.TOKEN=eyJraWQiOiJYa2pXdjMzTDRBYU1ZSzNGM...
-
Create a Spring Boot app with OAuth 2.0 support:
https start.spring.io/starter.zip \ bootVersion==3.0.0-RC1 \ dependencies==web,oauth2-resource-server,native \ packageName==com.okta.rest \ name==spring-boot \ type==maven-project \ baseDir==spring-boot | tar -xzvf -
-
Add a
HelloController
class that returns the user’s information: [sb-hello
]package com.okta.rest.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.security.Principal; @RestController public class HelloController { @GetMapping("/hello") public String hello(Principal principal) { return "Hello, " + principal.getName() + "!"; } }
-
Configure the app to be an OAuth 2.0 resource server by adding the issuer to
application.properties
.spring.security.oauth2.resourceserver.jwt.issuer-uri=https://{yourOktaDomain}/oauth2/default
-
Start your app from your IDE or using a terminal:
./mvnw spring-boot:run
-
Test your API with an access token.
http :8080/hello Authorization:"Bearer $TOKEN"
-
Compile your Spring Boot app into a native executable using the
native
profile:./mvnw native:compile -Pnative
TipTo build a native app and a Docker container, use the Spring Boot Maven plugin and ./mvnw spring-boot:build-image -Pnative
. -
Start your Spring Boot app:
./target/demo
-
Test your API with an access token.
http :8080/hello Authorization:"Bearer $TOKEN"
-
Run each image three times before recording the numbers, then each command five times
-
Write each time down, add them up, and divide by five for the average. For example:
Spring Boot: (39 + 40 + 38 + 37 + 41) / 5 = 39 Micronaut: (17 + 19 + 19 + 20 + 15) / 5 = 18 Quarkus: (25 + 18 + 20 + 19 + 21) / 5 = 20.6 Helidon: (45 + 44 + 45 + 39 + 43) / 5 = 43.2
Framework | Command executed | Milliseconds to start |
---|---|---|
Spring Boot |
|
39 |
Micronaut |
|
18 |
Quarkus |
|
20.6 |
Helidon |
|
43.2 |
Test the memory usage in MB of each app using the command below. Make sure to send an HTTP request to each one before measuring.
ps -o pid,rss,command | grep --color <executable> | awk '{$2=int($2/1024)"M";}{ print;}'
Substitute <executable>
as follows:
Framework | Executable | Megabytes before request | Megabytes after request | Megabytes after 5 requests |
---|---|---|---|---|
Spring Boot |
|
74 |
98 |
99 |
Micronaut |
|
43 |
58 |
69 |
Quarkus |
|
37 |
48 |
50 |
Helidon |
|
79 |
97 |
131 |
Important
|
If you disagree with these numbers and think X framework should be faster, I encourage you to clone the repo and run these tests yourself. If you get faster startup times for Spring Boot, do you get faster startup times for Helidon, Micronaut, and Quarkus too? |
⚡️ Create a secure REST API with Spring Boot:
okta start spring-boot
okta start spring-boot -b webflux
🚀 Find this example’s code on GitHub: @oktadev/native-java-examples/spring-boot
👀 Read the blog post: Build Native Java Apps with Micronaut, Quarkus, and Spring Boot