Steps to Install Keycloak Standalone Server

Prerequisites

Install Java

Install java (java-8-openjdk) in all the machines in the cluster and setup the JAVA_HOME environment variable for the same.

sudo yum install java-1.8.0-openjdk-devel

Get your Java installation path.

update-alternatives --display java

Take the value of the current link and remove the trailing /bin/java. For example, on RHEL 7, the link is /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre/bin/java, So, JAVA_HOME should be /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre.

Edit ~/bashrc.sh:

Export JAVA_HOME={path-tojava} with your actual java installation path. For example on a Debian with open-jdk-8:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre

Download & install keycloak

Download and unzip Keycloak

sudo wget "https://downloads.jboss.org/keycloak/6.0.1/keycloak-6.0.1.tar.gz" 
sudo tar xzf keycloak-6.0.1.tar.gz

Install a database supported by keycloak

We have installed postgres as the database for keycloak; you can use any database supported by Keycloak.

Documentation for Keycloak Database Setup is available here.

Install Postgres in your VM. Guide to install PostgreSQL is available here.

Within the …​/modules/ directory of your Keycloak distribution, you need to create a directory structure to hold your module definition. The convention is use the Java package name of the JDBC driver for the name of the directory structure. For PostgreSQL, create the directory org/postgresql/main. Copy your database driver JAR into this directory and create an empty module.xml file within it too.

Module Directory

After you have done this, open up the module.xml file and create the following XML:

Module XML

The module name should match the directory structure of your module. So, org/postgresql maps to org.postgresql. The resource-root path attribute should specify the JAR filename of the driver. The rest are just the normal dependencies that any JDBC driver JAR would have.

Create a service to start Keycloak

Enable SSL for Keycloak server

To enable SSL we need a certificate which here in example we will use Lets encrypt.

Follow the steps in this link to create a certificate for your domain.

We will create a keystore in which we will store certificate chain and private key and give them an alias

Configure standalone xml

  • Go to {{keycloak folder}}/standalone/configuration

  • Open Standalone.xml and make following changes

    • Add a driver for postgres(Or your database)

    • Change the datasource properties

    • Register the datasource While registering change the schema name if you want.

    • Change network configuration

      • Inet address for both public and management profile to access it remotely

      • Default ports from 8080 -> 80 and 8443 -> 443 to not give ports at time of accessing Keycloak

    • Adding a SSL certificate to Keycloak Here we will give the keystore we created to keycloak

Add keycloak admin user

Add Keycload admin user from keycloak bin directory run

Keycloak server start

Configure keycloak

  • Create a new Realm (eg. mosip).

  • Create clients for every module (i.e. ida, pre-registration, registration-processor, registration-client, auth, resident, mosip-client).

  • Enable authorization and service account for every client and provide valid redirect uri. These clients will be used by all modules to get client tokens.

Configure User Federation

For this example we will be configuring LDAP as user federation

  • Go to "User Federation".

  • Create a new User Federation for LDAP.

  • Make Edit Mode Writable.

  • Configure field based on your LDAP(There are many vendors for ldap you can connect to any ldap vendor based on configurations).

  • Go to Mappers and Create mappers for each field you want keycloak to take from LDAP.

  • Sync Users and Roles from LDAP .

  • Create INDIVIDUAL, RESIDENT Role from Keycloak in Realm Roles

  • Assign Roles from LDAP and Keycloak to All Clients

Access token expiration action

If you find that a particular service will take more time to complete the process within stipulated time period, your token perhaps will get invalidated. Use refresh token mechanism to get latest token or if that is not implemented you can increase the access token lifespan at client level or realm level.

SSL enable at keycloak

SSL in keycloak is enabled by default but it can be toggled for all request, external request, and none.

Update of configuration for keycloak

<> is for variable properties with this sign need to be updated.

Global configuration

Kernel configuration

Pre-registration configuration

Registration processor configuration

ID authentication configuration

Registration client configuration

Resident services configuration

Last updated

Was this helpful?