Client Credentials Flow with Micronaut and Auth0
Learn how to use Client Credentials Flow between Micronaut microservices with an Authorization Server provided by Auth0.
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:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately -
An Auth0 account.
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.
-
Download and unzip the source
Application Diagram
Download the complete solution of the guide. You will use the sample app as a starting point. The application contains three microservices:
-
bookcatalogue- This returns a list of books. It uses a domain consisting of a book name and an ISBN. -
bookinventory- This exposes an endpoint to check whether a book has sufficient stock to fulfil an order. It uses a domain consisting of a stock level and an ISBN. -
bookrecommendation- This consumes previous services and exposes an endpoint that recommends book names that are in stock.
The bookcatalogue service consumes endpoints exposed by the other services. The following image illustrates the original application flow:
A request to bookrecommendation (http://localhost:8080/books) triggers several requests through our microservices mesh.
In this guide, you are going to secure the communication between the microservices. You will use a client credentials flow and obtain an access token from an Auth0 authorization server.
OAuth 2.0
To provide authentication, sign in to your Auth0 account.
Create an application
Obtain client id and client secret
You can obtain the application’s domain, client id, and secret in the Auth0 console.
Obtain API audience
Go to Applications → APIs and copy the API Audience:
Authorize application
In the API Settings, authorize the application in the Machine to Machine Applications tab:
Writing the application
Dependencies
Modify every application (bookcatalogue, bookinventory, and bookrecommendation). Add Micronaut JWT and Micronaut OAuth 2.0 dependencies:
<dependency>
<groupId>io.micronaut.security</groupId>
<artifactId>micronaut-security-oauth2</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.micronaut.security</groupId>
<artifactId>micronaut-security-jwt</artifactId>
<scope>compile</scope>
</dependency>Changes to Book Inventory service
Annotate the controller’s method with @Secured:
To validate the tokens issued by Auth0, configure Validation with Remote JWKS:
micronaut.security.token.jwt.signatures.jwks.auth0.url=${OAUTH_JWKS:`https://micronautguides.eu.auth0.com/.well-known/jwks.json`}You can obtain the JWKS URL in the .well-known/openid-configuration endpoint.
Changes to Book Catalogue service
Annotate the controller’s method with @Secured:
To validate the tokens issued by Auth0, configure Validation with Remote JWKS:
micronaut.security.token.jwt.signatures.jwks.auth0.url=${OAUTH_JWKS:`https://micronautguides.eu.auth0.com/.well-known/jwks.json`}You can obtain the JWKS URL in the .well-known/openid-configuration endpoint.
Changes to Book Recommendations service
Books Controller Security
The GET /books endpoint in the bookrecommendation service is open.
Annotate the controller’s method with @Secured:
Configuration of HTTP Services URLs
Modify application-dev.properties to point the declarative HTTP clients to the other microservice URLs.
micronaut.http.services.bookcatalogue.url=http://localhost:8081
micronaut.http.services.bookinventory.url=http://localhost:8082Configuration
Add the following OAuth2 configuration:
The previous configuration uses several placeholders with default values. You will need to set up OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, and OAUTH_TOKEN_URL environment variables in your Auth0 application.
export OAUTH_CLIENT_ID=XXXXXXXXXX
export OAUTH_CLIENT_SECRET=YYYYYYYYYY
export OAUTH_TOKEN_URL=https://micronautguides.eu.auth0.com/oauth/tokenRunning the Application
Run bookcatalogue microservice
To run the application, execute ./mvnw mn:run.
...
14:28:34.034 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 499ms. Server Running: http://localhost:8081Run bookinventory microservice
To run the application, execute ./mvnw mn:run.
...
14:31:13.104 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 506ms. Server Running: http://localhost:8082Run bookrecommendation microservice
To run the application, execute ./mvnw mn:run.
...
14:31:57.389 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 523ms. Server Running: http://localhost:8080You can run a cURL command to test the whole application:
curl http://localhost:8080/books[{"name":"Building Microservices"}]Next Steps
Read Micronaut OAuth 2.0 Documentation to learn more.
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). |