Chapter 2. Protect a service application by using OpenID Connect (OIDC) Bearer token authentication
Use the Quarkus OpenID Connect (OIDC) extension to secure a Jakarta REST application with Bearer token authentication. The bearer tokens are issued by OIDC and OAuth 2.0 compliant authorization servers, such as Keycloak.
For more information about OIDC Bearer token authentication, see the Quarkus OpenID Connect (OIDC) Bearer token authentication guide.
If you want to protect web applications by using OIDC Authorization Code Flow authentication, see the OpenID Connect authorization code flow mechanism for protecting web applications guide.
2.1. Prerequisites Copy linkLink copied to clipboard!
To complete this guide, you need:
- Roughly 15 minutes
- An IDE
-
JDK 17+ installed with
JAVA_HOMEconfigured appropriately - Apache Maven 3.9.6
- A working container runtime (Docker or Podman)
- Optionally the Quarkus CLI if you want to use it
- Optionally Mandrel or GraalVM installed and configured appropriately if you want to build a native executable (or Docker if you use a native container build)
- The jq command-line processor tool
2.2. Architecture Copy linkLink copied to clipboard!
This example shows how you can build a simple microservice that offers two endpoints:
-
/api/users/me -
/api/admin
These endpoints are protected and can only be accessed if a client sends a bearer token along with the request, which must be valid (for example, signature, expiration, and audience) and trusted by the microservice.
A Keycloak server issues the bearer token and represents the subject for which the token was issued. Because it is an OAuth 2.0 authorization server, the token also references the client acting on the user’s behalf.
Any user with a valid token can access the /api/users/me endpoint. As a response, it returns a JSON document with user details obtained from the information in the token.
The /api/admin endpoint is protected with RBAC (Role-Based Access Control), which only users with the admin role can access. At this endpoint, the @RolesAllowed annotation is used to enforce the access constraint declaratively.
2.3. Solution Copy linkLink copied to clipboard!
Follow the instructions in the next sections and create the application step by step. You can also go straight to the completed example.
You can clone the Git repository by running the command git clone https://github.com/quarkusio/quarkus-quickstarts.git -b 3.8, or you can download an archive.
The solution is located in the security-openid-connect-quickstart directory.
2.4. Create the Maven project Copy linkLink copied to clipboard!
You can either create a new Maven project with the oidc extension or you can add the extension to an existing Maven project. Complete one of the following commands:
To create a new Maven project, use the following command:
Using the Quarkus CLI:
quarkus create app org.acme:security-openid-connect-quickstart \ --extension='oidc,resteasy-reactive-jackson' \ --no-code cd security-openid-connect-quickstartquarkus create app org.acme:security-openid-connect-quickstart \ --extension='oidc,resteasy-reactive-jackson' \ --no-code cd security-openid-connect-quickstartCopy to Clipboard Copied! Toggle word wrap Toggle overflow To create a Gradle project, add the
--gradleor--gradle-kotlin-dsloption.For more information about how to install and use the Quarkus CLI, see the Quarkus CLI guide.
Using Maven:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create a Gradle project, add the
-DbuildTool=gradleor-DbuildTool=gradle-kotlin-dsloption.
For Windows users:
-
If using cmd, (don’t use backward slash
\and put everything on the same line) -
If using Powershell, wrap
-Dparameters in double quotes e.g."-DprojectArtifactId=security-openid-connect-quickstart"
If you already have your Quarkus project configured, you can add the oidc extension to your project by running the following command in your project base directory:
Using the Quarkus CLI:
quarkus extension add oidc
quarkus extension add oidcCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Maven:
./mvnw quarkus:add-extension -Dextensions='oidc'
./mvnw quarkus:add-extension -Dextensions='oidc'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using Gradle:
./gradlew addExtension --extensions='oidc'
./gradlew addExtension --extensions='oidc'Copy to Clipboard Copied! Toggle word wrap Toggle overflow
This will add the following to your build file:
Using Maven:
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-oidc</artifactId> </dependency>
<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-oidc</artifactId> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using Gradle:
implementation("io.quarkus:quarkus-oidc")implementation("io.quarkus:quarkus-oidc")Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.5. Write the application Copy linkLink copied to clipboard!
Implement the
/api/users/meendpoint as shown in the following example, which is a regular Jakarta REST resource:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Implement the
/api/adminendpoint as shown in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe main difference in this example is that the
@RolesAllowedannotation is used to verify that only users granted theadminrole can access the endpoint.
Injection of the SecurityIdentity is supported in both @RequestScoped and @ApplicationScoped contexts.
2.6. Configure the application Copy linkLink copied to clipboard!
Configure the Quarkus OpenID Connect (OIDC) extension by setting the following configuration properties in the
src/main/resources/application.propertiesfile.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Where:
-
%prod.quarkus.oidc.auth-server-urlsets the base URL of the OpenID Connect (OIDC) server. The%prod.profile prefix ensures thatDev Services for Keycloaklaunches a container when you run the application in development (dev) mode. For more information, see the Run the application in dev mode section. -
quarkus.oidc.client-idsets a client id that identifies the application. -
quarkus.oidc.credentials.secretsets the client secret, which is used by theclient_secret_basicauthentication method.
For more information, see the Quarkus OpenID Connect (OIDC) configuration properties guide.
2.7. Start and configure the Keycloak server Copy linkLink copied to clipboard!
Put the realm configuration file on the classpath (
target/classesdirectory) so that it gets imported automatically when running in dev mode. You do not need to do this if you have already built a complete solution, in which case, this realm file is added to the classpath during the build.NoteDo not start the Keycloak server when you run the application in dev mode;
Dev Services for Keycloakwill start a container. For more information, see the Run the application in dev mode section.To start a Keycloak server, you can use Docker to run the following command:
docker run --name keycloak -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin -p 8180:8080 quay.io/keycloak/keycloak:{keycloak.version} start-devdocker run --name keycloak -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin -p 8180:8080 quay.io/keycloak/keycloak:{keycloak.version} start-devCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Where the
keycloak.versionis set to version24.0.0or later.
-
Where the
- You can access your Keycloak server at localhost:8180.
To access the Keycloak Administration console, log in as the
adminuser by using the following login credentials:-
Username:
admin -
Password:
admin
-
Username:
- Import the realm configuration file from the upstream community repository to create a new realm.
For more information, see the Keycloak documentation about creating and configuring a new realm.
2.8. Run the application in dev mode Copy linkLink copied to clipboard!
To run the application in dev mode, run the following commands:
Using the Quarkus CLI:
quarkus dev
quarkus devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Maven:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Gradle:
./gradlew --console=plain quarkusDev
./gradlew --console=plain quarkusDevCopy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Dev Services for Keycloak will start a Keycloak container and import a
quarkus-realm.json.
-
Open a Dev UI, which you can find at /q/dev-ui. Then, in an
OpenID Connectcard, click theKeycloak providerlink . When prompted to log in to a
Single Page Applicationprovided byOpenID Connect Dev UI, do the following steps:Log in as
alice(password:alice), who has auserrole.-
Accessing
/api/adminreturns a403status code. -
Accessing
/api/users/mereturns a200status code.
-
Accessing
Log out and log in again as
admin(password:admin), who has bothadminanduserroles.-
Accessing
/api/adminreturns a200status code. -
Accessing
/api/users/mereturns a200status code.
-
Accessing
2.9. Run the Application in JVM mode Copy linkLink copied to clipboard!
When you are done with dev mode, you can run the application as a standard Java application.
Compile the application:
Using the Quarkus CLI:
quarkus build
quarkus buildCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Maven:
./mvnw install
./mvnw installCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Gradle:
./gradlew build
./gradlew buildCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Run the application:
java -jar target/quarkus-app/quarkus-run.jar
java -jar target/quarkus-app/quarkus-run.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.10. Run the application in native mode Copy linkLink copied to clipboard!
You can compile this same demo as-is into native mode without any modifications. This implies that you no longer need to install a JVM on your production environment. The runtime technology is included in the produced binary and optimized to run with minimal resources required.
Compilation takes a bit longer, so this step is disabled by default.
Build your application again by enabling the
nativeprofile:Using the Quarkus CLI:
quarkus build --native
quarkus build --nativeCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Maven:
./mvnw install -Dnative
./mvnw install -DnativeCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using Gradle:
./gradlew build -Dquarkus.package.type=native
./gradlew build -Dquarkus.package.type=nativeCopy to Clipboard Copied! Toggle word wrap Toggle overflow
After waiting a little while, you run the following binary directly:
./target/security-openid-connect-quickstart-1.0.0-SNAPSHOT-runner
./target/security-openid-connect-quickstart-1.0.0-SNAPSHOT-runnerCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.11. Test the application Copy linkLink copied to clipboard!
For information about testing your application in dev mode, see the preceding Run the application in dev mode section.
You can test the application launched in JVM or native modes with curl.
- Because the application uses Bearer token authentication, you must first obtain an access token from the Keycloak server to access the application resources:
The preceding example obtains an access token for the user alice.
-
Any user can access the
http://localhost:8080/api/users/meendpoint, which returns a JSON payload with details about the user.
curl -v -X GET \ http://localhost:8080/api/users/me \ -H "Authorization: Bearer "$access_token
curl -v -X GET \
http://localhost:8080/api/users/me \
-H "Authorization: Bearer "$access_token
-
Only users with the
adminrole can access thehttp://localhost:8080/api/adminendpoint. If you try to access this endpoint with the previously-issued access token, you get a403response from the server.
curl -v -X GET \ http://localhost:8080/api/admin \ -H "Authorization: Bearer "$access_token
curl -v -X GET \
http://localhost:8080/api/admin \
-H "Authorization: Bearer "$access_token
-
To access the admin endpoint, obtain a token for the
adminuser:
For information about writing integration tests that depend on Dev Services for Keycloak, see the Dev Services for Keycloak section of the "OpenID Connect (OIDC) Bearer token authentication" guide.
2.12. References Copy linkLink copied to clipboard!
- OIDC configuration properties
- OpenID Connect (OIDC) Bearer token authentication
- Keycloak Documentation
- OpenID Connect
- JSON Web Token
- OpenID Connect and OAuth2 Client and Filters Reference Guide
- Dev Services for Keycloak
- Sign and encrypt JWT tokens with SmallRye JWT Build
- Combining authentication mechanisms
- Quarkus Security overview