This example app shows how to build a CRUD API with Java, MongoDB, and Spring Boot. See Build a CRUD API with Java and MongoDB to see how it was built.
Java 11: This project uses Java 11. If you don’t have Java 11, you can install OpenJDK. Instructions are found on the OpenJDK website.
Okta Developer Account: Go to the Okta website and sign up for a free developer account, if you haven’t already.
Okta has Authentication and User Management APIs that reduce development time with instant-on, scalable user infrastructure. Okta's REST API and makes it easy for developers to authenticate, manage, and secure users + roles in any application.
To install this example application, run the following commands:
git clone https://github.com/oktadeveloper/okta-java-mongodb-example.git
If you don't have an Okta developer account, please create one. Then, create a new OIDC app on Okta:
- Log in to your developer account, navigate to Applications > Add Application.
- Select Web > Next.
- Give the application a name
- Under Login redirect URIs, add a new URI:
https://oidcdebugger.com/debug
. - Under Grant types allowed, check Implicit (Hybrid).
- The rest of the default values will work. Click Done.
Steps 4 & 5 are only needed if you want to use the OpenID Connect Debugger to generate a test token.
You need to add the Issuer URI from your Okta account to the src/main/resources/application.properties
file. To find your Issuer URI, go to API -> Authorizaiton Servers.
okta.oauth2.issuer=https://{yourOktaUrl}/oauth2/default
To run the SpringMVC resource server, run:
./gradlew bootRun
Then use OIDC Debugger to create an access token. Save the access token as a variable in your terminal:
TOKEN=eyJraWQiOiJrQkNxZ3o1MmQtOUhVSl94c0x4aGtzYlJxUDVD...
Now you can make authenticated and authorized requests.
$ http :8080 "Authorization: Bearer $TOKEN"
HTTP/1.1 200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
...
{
"_links": {
"fridge": {
"href": "http://localhost:8080/fridge{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8080/profile"
}
}
}
Please post any questions as comments on the blog post or post them to Stack Overflow with the okta
tag.
Apache 2.0, see LICENSE.