Authorization in Micronaut

Published on January 19, 2024
Learning GoalLearn how to build a Micronaut resource server that's secured with OAuth 2.0.

In this lab, you will:

  • Learn how to build an API with Micronaut and Java.
  • Learn how to secure your API with OAuth 2.0.
  • 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 lab, 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.

Lab Setup

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 lab 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.

Once you sign in, Auth0 takes you to the Auth0 Dashboard, where you can configure and manage Auth0 assets, such as applications, APIs, connections, and user profiles.

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.

LOADING...

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!.

COMMAND
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:

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:

LOADING...

Populate it with the following code:

src/main/java/demo/HelloResource.java
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;
@Controller
public 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.

COMMAND
auth0 login
Visit the Access Your Tenant section of the "Auth0 CLI Basics" lab to learn more about the auth0 login command.

Then, run the command below to see your available Auth0 tenants:

COMMAND
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.

For security, these configuration values are stored in memory and only used locally. They are gone as soon as you refresh the page! As an extra precaution, you should use values from an Auth0 test application instead of a production one.

Now, update the content of the application.properties file to configure a resource server using your Auth0 domain:

src/main/resources/application.properties
micronaut.security.token.jwt.signatures.jwks.auth0.url=https://AUTH0-DOMAIN/.well-known/jwks.json

Start your application.

GRADLE
MAVEN
./gradlew run

Test Your API

Open a new terminal to test the /hello endpoint in the HelloResource.java file:

LOADING...

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:

COMMAND
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:

LOADING...

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 lab, 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?

COMMAND
mn create-app demo --features=security-jwt
auth0 tenants list
auth0 test token -a https://AUTH0-DOMAIN/api/v2/ -s openid
curl localhost:8080/hello -i --header "Authorization: Bearer AUTH0-ACCESS-TOKEN"

Check out our other Micronaut labs Authentication in Micronaut and Role Based Access Control in Micronaut to learn more about Auth0 security integration in Micronaut Java applications.