Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

7. Keycloak Setup

Nguyễn Xuân Nhân edited this page Oct 3, 2023 · 1 revision

Local Setup

Keycloak is an open-source identity and access management solution that allows you to secure your applications and services. This guide will walk you through the setup process for Keycloak.

Prerequisites

Before you begin, make sure you have the following prerequisites in place:

  1. Java: Keycloak is built on Java, so you need to have Java Development Kit (JDK) installed on your system. You can download it from Oracle or use OpenJDK.

  2. Database: Keycloak requires a database to store its configuration and user data. You can use databases like PostgreSQL, MySQL, or H2 (for development purposes).

  3. WildFly (Optional): If you want to deploy Keycloak as a standalone server, you can use WildFly as the application server. Download it from the WildFly website.

Installation

Follow these steps to set up Keycloak:

  1. Download Keycloak: Go to the Keycloak Downloads page and download the latest stable release.

  2. Extract the Archive: Extract the downloaded archive to your desired location on your server.

  3. Configure the Database: Depending on your chosen database, you'll need to set up a database and configure Keycloak to use it. Refer to the official Keycloak documentation for database-specific instructions.

  4. Configure Standalone.xml (WildFly): If you're using WildFly, open the standalone/configuration/standalone.xml file and configure the Keycloak subsystem. Refer to the Keycloak documentation for detailed instructions.

  5. Start Keycloak: Start Keycloak by running the appropriate script for your operating system. For example, on Unix-based systems, you can run:

bin/standalone.sh

On Windows:

bin/standalone.bat
  1. Access the Admin Console: Once Keycloak is up and running, you can access the admin console by navigating to http://localhost:8080/auth/admin/ in your web browser. You can log in with the admin credentials you set during the initial setup.

  2. Create Realms and Clients: In the admin console, you can create realms to separate your applications' security domains and configure clients for your applications.

  3. Integrate Applications: Integrate your applications with Keycloak for authentication and authorization. Keycloak provides various client libraries and adapters for different platforms and languages.

  4. User Management: You can manage users, roles, and groups through the admin console or Keycloak's REST API.

Container Setup (Docker)

To run Keycloak in a Docker container, follow these steps:

  1. Pull the Keycloak Docker Image: Open your terminal and run the following command to pull the official Keycloak Docker image:
docker pull jboss/keycloak
  1. Create a Docker Network (Optional): You can create a Docker network to isolate Keycloak from other containers if needed:
docker network create keycloak-network
  1. Run Keycloak Container: Start a Keycloak container using the following command:
docker run -d --name keycloak \
  -e KEYCLOAK_USER=admin \
  -e KEYCLOAK_PASSWORD=admin \
  -p 8090:8080 \
  --network keycloak-network \
  jboss/keycloak
  1. Access the Admin Console: Once the container is running, you can access the Keycloak admin console by navigating to http://localhost:8090 in your web browser. Use the admin credentials you set during container startup.

Note: If you use docker-compose, please skip the above steps

Configuration

If you install Keycloak using Docker, you won't need to create a new administration account because there is already one with the username admin and the password admin. Your admin console will appear as follows:

image

image

Note: If you are utilizing Docker Compose in your project, all the necessary configurations will be automatically imported into your Keycloak server. Therefore, you can skip all the steps for configuring Keycloak below if you prefer not to configure it manually.

Create a client

image

image

image

Create roles

Role Description
Admin Admins manage operations, user accounts, and technology for smooth business functioning
Driver Drivers operate vehicles for transportation
Stoker Stokers tend to fires to produce steam or heat
Officer Officers enforce rules and regulations
Customer Customers make purchases or receive services

image

Create client scopes

image

image

Create user

image

image

image