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
|
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.
If you use another browser, or an incognito window, you’ll be prompted to sign in first.
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:
Changelog:
- Jan 25, 2022:
Updated to use
.okta.env
instead ofapplication.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.
Okta Developer Blog Comment Policy
We welcome relevant and respectful comments. Off-topic comments may be removed.