Groovy / Gradle

JWK Generation with a Micronaut Command Line Application

Learn how to generate a JSON Web Key (JWK) with a Micronaut CLI (command-line interface) application

Sergio del Amo
On this guide
In this section

Getting Started

In this guide, we will create a Micronaut application written in Groovy.

What you will need

To complete this guide, you will need the following:

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Writing the App

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-cli-app example.micronaut.micronautguide --build=gradle --lang=groovy
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

Code

JSON Web Key Generation

Create an interface to encapsulate the contract to generate a JWK (JSON Web Key)

groovy/src/main/groovy/example/micronaut/JsonWebKeyGenerator.groovy
package example.micronaut

import io.micronaut.core.annotation.NonNull
import io.micronaut.core.annotation.Nullable

/**
 * <a href="https://datatracker.ietf.org/doc/html/rfc7517">JSON Web Key</a>
 */
interface JsonWebKeyGenerator {

    @NonNull
    Optional<String> generateJsonWebKey(@Nullable String kid)
}

To generate a JWK, use Nimbus JOSE + JWT, an open source Java library to generate JSON Web Tokens (JWT).

Add the following dependency:

build.gradle
implementation("com.nimbusds:nimbus-jose-jwt:@nimbus-jose-jwtVersion@")

Create an implementation of JsonWebKeyGenerator

groovy/src/main/groovy/example/micronaut/RS256JsonWebKeyGenerator.groovy

CLI Command

Micronaut CLI applications use Picocli.

Replace the contents of MicronautguideCommand:

groovy/src/main/groovy/example/micronaut/MicronautguideCommand.groovy

Replace the contents of MicronautguideCommandTest:

groovy/src/test/groovy/example/micronaut/MicronautguideCommandSpec.groovy

Testing the Application

To run the tests:

./gradlew test

Then open build/reports/tests/test/index.html in a browser to see the results.

Running the CLI App

Create an executable jar including all dependencies:

./gradlew shadowJar

Execute the CLI with the help argument.

java -jar build/libs/micronautguide-0.1-all.jar --help
12:16:33.257 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [cli]
Usage: keysgen [-hV] [-kid=<kid>]
Generates a JSON Web Key (JWK) with the RS256 algorithm.
  -h, --help       Show this help message and exit.
      -kid=<kid>   Key ID. Parameter is used to match a specific key. If not
                     specified a random Key ID is generated.
  -V, --version    Print version information and exit.

Next Steps

Read Picocli documentation.

Explore more features with Micronaut Guides.

Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

License

Note
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).