Spring Boot and Okta in 2 Minutes

Spring Boot and Okta in 2 Minutes

The Okta CLI is a new tool we’ve created here at Okta. It’s designed to streamline the process of creating new Okta accounts, registering apps, and getting started. Wwwhhaaattt, you might say?! That’s right, it’s super awesome!

To show you how much fun it is, I created a screencast that shows you how to use it.

This video puts your settings in src/main/resources/application.properties. We’ve since changed the default behavior to use spring-dotenv.

For those that would rather read than watch, please read on.

Install the Okta CLI

You can find the website for the Okta CLI at cli.okta.com. This site has instructions on how to use common package managers on macOS, Windows, and Linux. You can also use cURL:

curl https://raw.githubusercontent.com/okta/okta-cli/master/cli/src/main/scripts/install.sh | bash

Once you have the Okta CLI installed, you’ll need Java 11 installed to run your Spring Boot app.

Create a 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 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

Follow the instructions that are printed out for you. To summarize:

cd spring-boot
./mvnw spring-boot:run

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

Hello Matt

If you use another browser, or an incognito window, you’ll be prompted to sign in first.

Okta Login

If you open src/main/java/com/example/sample/Application.java, you’ll see the Java code that’s used to render your name. You might appreciate how Spring Security makes authentication with OpenID Connect easy.

@RestController
static class SimpleRestController {
    @GetMapping("/")
    String sayHello(@AuthenticationPrincipal OidcUser oidcUser) {
        return "Hello: " + oidcUser.getFullName();
    }
}

Learn More about Spring Boot and Okta

I hope you’ve enjoyed this brief intro to the Okta CLI. It’s a tool for developers to make their lives easier. If you have any suggestions for improvement, please add an issue to our okta/okta-cli repository.

If you like Spring Boot and Okta, you might like these posts:

Be sure to follow us @oktadev on Twitter, YouTube and Twitch. We’re accustomed to publishing fantastic content!

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#1048.

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.