Authorization in Micronaut
Updated on June 7, 2024The focus of this Micronaut tutorial is to help developers learn how to build a Micronaut resource server that’s secured with OAuth 2.0. The examples in this tutorial cover the following concepts:
- How to build an API with Micronaut and Java.
- How to secure your API with OAuth 2.0.
- How to test your protected API endpoints.
Why Use Micronaut to Build APIs?
Micronaut is a modern, JVM-based framework designed for building modular, easily testable microservices and serverless applications. It focuses on faster start-up time, high throughput, and minimal memory consumption. It is cloud-native by default and includes a built-in testing framework.
The OAuth 2.0 authorization framework is a protocol that allows a user to grant a third-party website or application access to the user's protected resources without necessarily revealing their long-term credentials or even their identity.
In this guide, you'll learn how to use Java and Micronaut to build a REST API that's secured with OAuth 2.0. You'll also learn how to make authenticated requests to test if it works as expected.
Set up a Development Environment
- Use your favorite text editor or IDE. We recommend using IntelliJ IDEA.
- Ensure that you have Java
17+
installed in your system. You can easily install it using SDKMAN!. - Windows commands in this guide are written for PowerShell.
Create an Auth0 account
If you already have an Auth0 account, you can log in to your tenant and continue to the next step.
Otherwise, sign up for a free Auth0 account.
During the sign-up process, you create something called an Auth0 Tenant, where you configure your use of Auth0.
Set up the Auth0 CLI
If you are not familiar with the Auth0 CLI, you can follow the "Auth0 CLI Basics" lab to learn how to build, manage, and test your Auth0 integrations from the command line.
There are different ways to install the Auth0 CLI, depending on your operating system.
Install the Micronaut CLI
The Micronaut CLI is a command-line tool that helps you create, manage, and build Micronaut applications. You can install it using SDKMAN!.
sdk install micronaut
For more installation options, see the Micronaut documentation.
Create a Micronaut REST API Server
To create a new project using the Micronaut CLI, run the following command:
mn create-app demo --features=security-jwt
This will create a new project with Gradle as the build tool. If you prefer to use Maven, you can add --build=maven
to the above command.
If you prefer to use a browser, navigate to https://micronaut.io/launch/ and create a Micronaut project with security-jwt
feature.
Create a HelloResource.java
class next to Application.java
:
Populate it with the following code:
package demo;import java.security.Principal;import io.micronaut.http.MediaType;import io.micronaut.http.annotation.Controller;import io.micronaut.http.annotation.Get;import io.micronaut.http.annotation.Produces;import io.micronaut.security.annotation.Secured;import io.micronaut.security.rules.SecurityRule;@Controllerpublic class HelloResource {@Secured(SecurityRule.IS_AUTHENTICATED)@Produces(MediaType.TEXT_PLAIN)@Get("/hello")public String hello(Principal principal) {return "Hello, " + principal.getName() + "!";}}
Secure Your API with OAuth 2.0
In a terminal, connect the Auth0 CLI to your Auth0 tenant.
auth0 login
auth0 login
command.Then, run the command below to see your available Auth0 tenants:
auth0 tenants list
Copy the domain of the Auth0 tenant that you'd like to use and paste it into the following input box:
When you enter a value in the input fields present on this page, any code snippet that uses such value updates to reflect it. Using the input fields makes copying and pasting code as you follow along easy.
Now, update the content of the application.properties
file to configure a resource server using your Auth0 domain:
micronaut.security.token.jwt.signatures.jwks.auth0.url=https://AUTH0-DOMAIN/.well-known/jwks.json
Start your application.
./gradlew run
Test Your API
Open a new terminal to test the /hello
endpoint in the HelloResource.java
file:
You will get a 401 Unauthorized
response because you didn't pass in an access token.
Get an access token
You can get an access token using the Auth0 CLI to test making a secure call to your protected API endpoint:
auth0 test token -a https://AUTH0-DOMAIN/api/v2/ -s openid
Select any available client when prompted. You also will be prompted to open a browser window and log in with a user credential. You can sign up as a new user using an email and password or using the Google social login.
Paste the access token value in the following field so that you can use it to test your resource server:
Run the following command to make an authenticated request to your resource server:
You should receive a 200 OK
response with a message similar to the following:
Hello, auth0|61bcbc76f64d4a0072af8a1d!
Stop the resource server using Ctrl+C
.
Recap
In this guide, you learned how to build a Java REST API with Micronaut, secure it with OAuth 2.0, and learn how to make authenticated requests using the tool of your preference. If you'd like to see what's in the access token you created, you can copy and paste it into JWT.io. Isn't it cool how you can create and test a Micronaut API with Auth0 so quickly?
mn create-app demo --features=security-jwtauth0 tenants listauth0 test token -a https://AUTH0-DOMAIN/api/v2/ -s openidcurl localhost:8080/hello -i --header "Authorization: Bearer AUTH0-ACCESS-TOKEN"
Check out our other Micronaut guides Authentication in Micronaut and Role Based Access Control in Micronaut to learn more about Auth0 security integration in Micronaut Java applications.