Skip to main content

Keycloak JWT Configuration

How to configure a Keycloak realm for JWT.


warning

If you switch from the default configuration to JSON Web Tokens, all API keys generated in the API Portal will be turned off.

Requirements

Please ensure you have the following:

  • A Keycloak instance with all the required permissions
  • A Keycloak realm
  • Admin permissions for the Traefik Hub workspace
  • curl

Client Configuration

In the first step, select the realm and select Create client to create a new Keycloak client.

The example uses the realm keycloak-demo and the name of the new client will be new-client.

Choose realm and create a new client

Create new client scope

Create a Client Scope

A client scope is a way to limit the roles that get declared inside an access token. When a client requests that a user be authenticated, the access token they receive back will only contain the role mappings you’ve explicitly specified for the client’s scope.

This allows you to limit the permissions each individual access token has, rather than giving the client access to all the user’s permissions.

Make sure to disable the full group path setting.

Disable full group path setting

Test the Configuration

Use curl to generate a JWT and validate it against your Keycloak instance.

The following example uses the password grant type as a validation method.

CLI
curl -s \
--data-urlencode 'username=jane.doe' \
--data-urlencode 'password=&MY-SECURE-PASSWORD' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=jwt-demo' \
https://$KEYCLOAK-URL/realms/keycloak-demo/protocol/openid-connect/token
{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2OTk1MTg5NDYsImlhdCI6MTY5OTUxODY0NiwianRpIjoiN2U5ZTUxYjQtN2M5Ny00MjdiLWIyZWMtYzA1NGIyMGFiMDMwIiwiaXNzIjoiaHR0cHM6Ly9rZXljbG9hay5leGFtcGxlLmlvL3JlYWxtcy9rZXljbG9hay1kZW1vIiwiYXVkIjoiYWNjb3VudCIsInN1YiI6IjM0MTYwNWIyLWU0NGItNGNkMC1hNDZjLWEwNjMzNWFhYTVkYiIsInR5cCI6IkJlYXJlciIsImF6cCI6Imp3dC1kZW1vIiwic2Vzc2lvbl9zdGF0ZSI6IjU2MDQwZmE3LTJiZjQtNGRhZi05YjhhLWEzZjRmZjhhZWM3MSIsImFjciI6IjEiLCJhbGxvd2VkLW9yaWdpbnMiOlsiLyoiXSwicmVhbG1fYW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