Navigate back to the homepage

Setting up Keycloak for a multi-tenant app

Johannes Matt
October 11th, 2022 · 1 min read

The following steps describe how to set up Keycloak as your IDP for a multi-tenant application. If you want to quickly set up and run a local Keycloak instance, you can easily do this with this docker-compose.yml file.

Start the Keycloak instance with docker-compose up -d and go to http://localhost:9090/admin to log in to the Admin UI (use the configured admin credentials from the .env file).

Table of Contents

In the following steps, we will set up two tenants: tenant 1 and tenant 2. We will add two users for each tenant (alice and alex for tenant 1, bob and bella for tenant 2).

Login

Keycloak Login

Create realm tenant-1

Keycloak - Create realm 1
Keycloak - Create realm 2

Create client app

Keycloak - Create client 1
Keycloak - Create client 2
Keycloak - Create client 3
Keycloak - Create client 4

Create role user

Keycloak - Create role 1
Keycloak - Create role 2

Create group users

Keycloak - Create group 1
Keycloak - Create group 2

Assign user role mapping to group users

Keycloak - Assign role mapping 1
Keycloak - Assign role mapping 2

Create users alice and alex

Keycloak - Create user 1
Keycloak - Create user 2
Keycloak - Create user 3
Keycloak - Create user 4
Keycloak - Create user 5

Now that we have set up the structure for tenant 1, we need to repeat the same steps for setting up tenant 2. We can follow the above steps in the same way to set up the tenant-2 realm with the users bob and bella.

Conclusion

With the provided instructions in this post, we have shown how to set up Keycloak and prepare it for usage in a multi-tenant environment. To see how to integrate Keycloak with a multi-tenant Spring app, check out the post How to make your multi-tenant Spring app production-ready.

More articles from Johannes Matt

How to build a multi-tenant SaaS solution with Spring

In the second part of our multi-tenant SaaS series, we will implement an OAuth2 resource server using Spring Security

April 12th, 2022 · 3 min read

How to design a multi-tenant SaaS solution

In this new series of blog posts, we will look at how to design and build a multi-tenant SaaS solution from scratch

February 1st, 2022 · 5 min read
© 2022–2023 Johannes Matt
Link to $https://linkedin.com/in/johannes-mattLink to $https://github.com/jomatt