Check out the free virtual workshops on how to take your SaaS app to the next level in the enterprise-ready identity journey!

Spring Native in Action with the Okta Spring Boot Starter

Spring Native in Action with the Okta Spring Boot Starter

In the fall of 2020, the Spring team released a new experimental Spring Native project that gave Spring developers hope for faster startup times. Spring Native is all about converting your Spring applications to native executables. It leverages GraalVM to make it happen. This announcement was huge because the new kids on the block, Micronaut and Quarkus, produced native executables by default.

I was really excited about Spring Native when I first heard about it. Its first release (v0.8.3) was on Nov 23, 2020, but I started playing with it in September 2020, probably due to SpringOne. How do I know this? Because that’s when I first tried it, and entered an issue for the Okta Spring Boot starter. I was able to get things working with Spring Security’s OAuth dependencies (instead of the Okta starter) in the waning weeks of 2020.

Branching out with Spring Native

According to VMWare Tanzu’s recent State of Spring 2021 report, a lot of people know about Spring Native, but it’s still early.

State of Spring Slide

I wrote about how to build native Java apps with Micronaut, Quarkus, and Spring Boot earlier this summer and included my learnings. Around the same time, I thought to myself, "it’d sure be nice if the Okta Spring Boot starter worked with Spring Native."

To solve this desire, I scheduled a Twitch stream with Brian Demers and Josh Long. We spent 90+ minutes figuring things out. You can watch our struggles, collaborations, and ultimate joy in the video below. I added a detailed table of contents to the video’s description on YouTube, so you can skip around if you like.

This stream originally aired on June 22, 2021. Since then, there have been a few Spring Boot releases and several Spring Native releases. And don’t forget, there was another SpringOne!

Okta Spring Boot Starter v2.1.1+ Supports Spring Native!

Today, I’m happy to announce that we’ve added our learnings to Okta Spring Boot v2.1.1; now, it only takes a couple of lines to add native support to a Spring Boot application that uses Okta.

import org.springframework.nativex.hint.NativeHint;
@NativeHint(options = "--enable-https")

Or, if you’d prefer a full example:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.nativex.hint.NativeHint;

@NativeHint(options = "--enable-https")
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

If you’d like to see what it takes to migrate my previously mentioned native Java + Spring Boot example from using Spring Security OAuth to the Okta starter, look no further than @oktadev/native-java-examples#5.

You can also try it out yourself with the Okta CLI. Install it and follow along below for a quick Okta + Spring Native example.

Create a Native Spring Boot App

To create a secure Spring Boot app with Okta, run okta start spring-boot. You’ll need to verify your email and set a password as part of this.

If you already have an Okta account, you can run okta login first.

This command will download our Okta Spring Boot sample, register your app on Okta, and configure it by adding your Okta settings to a .okta.env file.

Keep your secrets out of source control by adding okta.env to your .gitignore file:

echo .okta.env >> .gitignore

Add @NativeHint(options = "--enable-https") to the main Application class as specified above.

Next, edit your pom.xml and add the Spring Native version and classifier to the <properties> section:

<spring-native.version>0.10.4</spring-native.version>
<repackage.classifier/>

Then, replace the <build> section with the XML below:

<build>
    <defaultGoal>spring-boot:run</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>${repackage.classifier}</classifier>
                <image>
                    <builder>paketobuildpacks/builder:tiny</builder>
                    <env>
                        <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                    </env>
                </image>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.experimental</groupId>
            <artifactId>spring-aot-maven-plugin</artifactId>
            <version>${spring-native.version}</version>
            <executions>
                <execution>
                    <id>test-generate</id>
                    <goals>
                        <goal>test-generate</goal>
                    </goals>
                </execution>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/release</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

Giddyup!

./mvnw spring-boot:build-image
# wait until image builds
docker run -it -p8080:8080 okta-spring-boot-sample:0.0.1-SNAPSHOT

Next, open your browser to http://localhost:8080. You’ll likely be logged in straight away and see your name printed on the screen.

Learn More About Spring Boot and Spring Native

We hope you learned something from this video and our experience. A huge thanks to Josh Long and Andy Clement for their assistance during this stream. The Spring Native docs are where you want to go to really dig in. If you prefer videos, I recommend watching Josh Long’s Spring Tips: Spring Native 0.10.0 video.

Check out these posts for more information about Spring Boot and Spring Native:

If you have any questions about this post, please add a comment below. For more interesting content, follow @oktadev on Twitter, connect with us on LinkedIn, and subscribe to our YouTube channel.

Changelog:

  • Jan 25, 2022: Updated to use .okta.env instead of application.properties. This change is necessary since the okta-spring-boot-sample now uses dotenv to load Okta settings. Changes to this post can be viewed in okta-blog#1049.

Matt Raible is a well-known figure in the Java community and has been building web applications for most of his adult life. For over 20 years, he has helped developers learn and adopt open source frameworks and use them effectively. He's a web developer, Java Champion, and Developer Advocate at Okta. Matt has been a speaker at many conferences worldwide, including Devnexus, Devoxx Belgium, Devoxx France, Jfokus, and JavaOne. He is the author of The Angular Mini-Book, The JHipster Mini-Book, Spring Live, and contributed to Pro JSP. He is a frequent contributor to open source and a member of the JHipster development team. You can find him online @mraible and raibledesigns.com.

Okta Developer Blog Comment Policy

We welcome relevant and respectful comments. Off-topic comments may be removed.