In this demo, I’ll show how to create a secure REST API and native image with Quarkus. 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, Spring Boot, 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...
-
Use Maven to generate a new Quarkus app with JWT support:
mvn io.quarkus:quarkus-maven-plugin:2.13.3.Final:create \ -DprojectGroupId=com.okta.rest \ -DprojectArtifactId=quarkus \ -DclassName="com.okta.rest.quarkus.HelloResource" \ -Dpath="/hello" \ -Dextensions="smallrye-jwt,resteasy-reactive"
-
Edit
src/java/com/okta/rest/quarkus/HelloResource.java
and add user information to thehello()
method: [qk-hello
]package com.okta.rest.quarkus; import io.quarkus.security.Authenticated; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.SecurityContext; import java.security.Principal; @Path("/hello") public class HelloResource { @GET @Authenticated @Produces(MediaType.TEXT_PLAIN) public String hello(@Context SecurityContext context) { Principal userPrincipal = context.getUserPrincipal(); return "Hello, " + userPrincipal.getName() + "!"; } }
-
Add your Okta endpoints to
src/main/resources/application.properties
: [qk-properties
]mp.jwt.verify.issuer=https://{yourOktaDomain}/oauth2/default mp.jwt.verify.publickey.location=${mp.jwt.verify.issuer}/v1/keys
-
Modify the
HelloResourceTest
to expect a 401 instead of a 200:package com.okta.rest.quarkus; import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; @QuarkusTest public class HelloResourceTest { @Test public void testHelloEndpoint() { given() .when().get("/hello") .then() .statusCode(401); } }
-
Run your Quarkus app:
./mvnw quarkus:dev
-
Test it from another terminal:
http :8080/hello
-
Test with 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:
Quarkus: (25 + 18 + 20 + 19 + 21) / 5 = 20.6 Micronaut: (17 + 19 + 19 + 20 + 15) / 5 = 18 Spring Boot: (39 + 40 + 38 + 37 + 41) / 5 = 39 Helidon: (45 + 44 + 45 + 39 + 43) / 5 = 43.2
Framework | Command executed | Milliseconds to start |
---|---|---|
Quarkus |
|
20.6 |
Micronaut |
|
18 |
Spring Boot |
|
39 |
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 |
---|---|---|---|---|
Quarkus |
|
37 |
48 |
50 |
Micronaut |
|
43 |
58 |
69 |
Spring Boot |
|
74 |
98 |
99 |
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 Quarkus, do you get faster startup times for Helidon, Micronaut, and Spring Boot too? |
⚡️ Create a secure REST API with Quarkus: okta start quarkus
🚀 Find this example’s code on GitHub: @oktadev/native-java-examples/quarkus
👀 Read the blog post: Build Native Java Apps with Micronaut, Quarkus, and Spring Boot