Skip to content
Menu
DevSecOps Now!!!
  • About
  • Certifications
  • Contact
  • Courses
  • DevSecOps Consulting
  • DevSecOps Tools
  • Training
  • Tutorials
DevSecOps Now!!!

Keycloak Realm Roles: A Comprehensive Guide for Developers

Posted on October 7, 2025

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Keycloak is an open-source identity and access management solution. It allows you to secure applications and services by managing users, roles, and permissions. One of the essential concepts in Keycloak is realm roles, which provide a global method for managing user permissions within a specific realm. This tutorial will walk you through the concept of realm roles, how to configure them, how to assign them to users, and common errors that developers encounter when working with realm roles in Keycloak.


Introduction to Keycloak Realm Roles

A realm in Keycloak is a space where you manage users, applications (clients), roles, and permissions. Realm roles are roles that apply globally within a realm, meaning they can be used across multiple clients (applications) within that realm. Unlike client roles (which are specific to an individual application), realm roles are shared across all the applications within the same realm.

Benefits of Realm Roles:

  • Global Permissions: Realm roles are not tied to a specific application, making them ideal for global permissions that should be recognized across multiple applications.
  • Role Management: Simplifies role management by allowing you to assign and manage permissions at the realm level instead of individual client level.
  • RBAC: Realm roles are often used in Role-Based Access Control (RBAC) to define what a user can or cannot do within the realm.

Setting Up Keycloak

Before diving into realm roles, ensure that your Keycloak instance is set up. If you don’t have Keycloak installed yet, follow these steps:

Step-by-Step Keycloak Setup:

  1. Download and Run Keycloak:
    • Download the latest version of Keycloak from the official website.
    • Run Keycloak using the command: ./bin/standalone.sh
  2. Access Keycloak Admin Console:
    • Open your browser and go to http://localhost:8080/auth to access the Keycloak Admin Console.
    • The default username is admin, and the password is set during the first setup.
  3. Create a Realm:
    • Log into the Admin Console, go to the Master drop-down in the top-left corner, and select Add Realm.
    • Enter a name for your new realm and save.

Creating Realm Roles

Steps to Create Realm Roles:

  1. Navigate to the Roles Section:
    • In the Keycloak Admin Console, select your realm from the top-left corner.
    • Go to Roles in the left navigation panel.
    • Click Add Role.
  2. Create a Role:
    • Provide a name for your role (e.g., “admin”, “manager”, “user”).
    • Optionally, provide a description to make the role’s purpose clear.
    • Click Save.
  3. Composite Roles:
    • If you need to combine multiple roles, you can create a composite role that contains other roles inside it.
    • To do this, when creating a role, select the Composite Roles tab and assign other roles as part of the new role.

Example:

Letโ€™s create a role called admin:

  • Role Name: admin
  • Description: Admin role with full access to all resources

Assigning Realm Roles to Users

Once the realm roles are created, you can assign them to individual users.

Steps to Assign Realm Roles to Users:

  1. Navigate to Users Section:
    • In the Admin Console, go to Users and search for the user you want to assign the role to.
    • Select the user and click on the Role Mappings tab.
  2. Assign Roles:
    • Under the Available Roles section, select the roles you want to assign (e.g., admin).
    • Click Add Selected to assign the role to the user.
  3. Verify Role Assignment:
    • Once assigned, the role will appear under Assigned Roles.

Using Realm Roles in Applications (Clients)

Once a realm role is assigned to a user, it can be used to control access to resources in applications (clients) that are configured within the realm. This is where Role-Based Access Control (RBAC) comes into play.

Steps to Use Realm Roles in Applications:

  1. Map Realm Roles to Client Roles:
    • Navigate to the Clients section in the Admin Console.
    • Select the client (application) you want to map the realm role to.
    • Go to the Role Mappings tab and add the desired realm role.
  2. Read Roles in Application Code:
    • In your application, you can read the roles from the JWT (JSON Web Token) issued by Keycloak.
    • The roles are typically present in the realm_access claim in the token.

Example of JWT with Realm Roles:

{
  "realm_access": {
    "roles": ["admin", "user"]
  }
}

In your application, you can check if the user has the admin role and grant access accordingly.


Role Mapping and Role Inheritance

Keycloak allows role inheritance through composite roles. A composite role can contain other roles, which makes role management more efficient.

Example of Composite Roles:

  • Create a composite role called superadmin which includes the admin and manager roles.

Benefits of Role Inheritance:

  • Simplifies role assignments by grouping related roles together.
  • Ensures that users who require multiple permissions can have them all assigned through a single role.

Working with Role-Based Access Control (RBAC)

Role-based access control (RBAC) is a critical feature when using realm roles. RBAC allows you to define what resources a user can access based on the roles they have been assigned.

Steps to Implement RBAC with Keycloak:

  1. Define Role-Based Permissions:
    • Identify the different roles (e.g., admin, user, manager) in your application.
    • For each role, define the permissions required (e.g., read-only access, admin access).
  2. Map Roles to Resources:
    • Assign specific roles to users based on the resources or actions they should be able to access.

Common Errors and Troubleshooting

Here are some common errors developers encounter when working with realm roles in Keycloak, along with troubleshooting tips:

Error 1: “Roles are not being passed in the token”

  • Cause: The realm roles are not included in the JWT token.
  • Solution: Ensure that the client has the correct settings to include realm roles in the token. In the Client settings, make sure Client Scopes are configured to include roles.

Error 2: “Role not found”

  • Cause: The role might not be created or assigned properly.
  • Solution: Double-check that the role exists in the Roles section and ensure that itโ€™s correctly assigned to the user.

Error 3: “Permission denied, user doesnโ€™t have the required role”

  • Cause: The user doesnโ€™t have the correct roles assigned or they are not mapped properly.
  • Solution: Verify that the role is correctly assigned to the user. Check the Role Mappings tab under the user profile to ensure the role is correctly assigned.

Error 4: “Role not recognized by client”

  • Cause: The client is not recognizing the realm role because it has not been mapped to a client role.
  • Solution: Go to the Clients section and ensure that the realm role is mapped to the client role.

Best Practices and Security Considerations

  • Use Composite Roles: For efficient role management, use composite roles to group multiple roles together.
  • Limit Roles: Assign only necessary roles to users. Following the principle of least privilege will minimize security risks.
  • Regular Audits: Periodically review user roles and permissions to ensure that no unnecessary roles are assigned to users.
  • Token Expiry: Set appropriate token expiry and refresh intervals to mitigate the risk of compromised tokens.

Conclusion

Keycloak provides a powerful and flexible mechanism for managing roles and permissions using realm roles. By defining roles at the realm level, you can easily manage user access to resources across multiple applications. This tutorial has covered everything from creating and assigning realm roles to troubleshooting common errors. By following best practices and understanding the underlying mechanisms, you can secure your applications effectively using Keycloak.

For more advanced use cases, consider integrating with client roles, composite roles, and Role-Based Access Control (RBAC) to provide even more granular access control.


Post Views: 2,614
  • Assigning Realm Roles to Users
  • Creating Realm Roles
  • Keycloak
  • Keycloak Comprehensive Guide for Developers
  • Keycloak Realm Roles
  • Keycloak Realm Roles: A Comprehensive Guide for Developers
  • Realm Roles
  • Realm Roles to Users
Subscribe
Login
Notify of
guest
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
  • Certified Site Reliability Architect: The Complete Career Guide
  • What Is a VPN? A Complete Beginner-to-Advanced Tutorial
  • How to Install, Secure, and Tune MySQL 8.4 on Ubuntu 24.04 for Apache Event MPM and PHP-FPM
  • Complete Guide to Certified Site Reliability Engineer Career
  • Certified DevSecOps Professional Step by Step
  • Certified DevSecOps Manager: Complete Career Guide
  • Certified DevSecOps Engineer: Skills, Career Path and Certification Guide
  • Step-by-Step: Become a Certified DevSecOps Architect
  • Tuning PHP 8.3 for Apache Event MPM and PHP-FPM on Ubuntu: A Complete Step-by-Step Production Guide
  • Complete Step-by-Step Guide to Configure Apache Event MPM, Create index.php, Set Up VirtualHost, and Fix Ubuntu Default Page
  • Convert XAMPP Apache to Event MPM + System PHP-FPM
  • The Gateway to System Observability Engineering (MOE)
  • How to Finetune Apache and Prove It Works: A Real-World Guide to Testing Performance, Concurrency, HTTP/2, Memory, CPU, and Security
  • Building a High-Performance Apache Event MPM + PHP-FPM + MariaDB Stack (Advanced Server Optimization Guide)
  • Master Infrastructure as Code: The Complete Hashicorp Terraform Associate Guide
  • Building a High-Performance Apache Server with Event MPM + PHP-FPM (Step-by-Step Guide)
  • Is XAMPP Safer for Production Than Using Apache and PHP as Root? 2026 Practical Guide
  • Unlock Cloud Security Expertise with Certified Kubernetes Security Specialist (CKS)
  • How to Fix wpDiscuz Not Replacing Default WordPress Comments in Block Themes
  • Complete Guide to Certified Kubernetes Application Developer Certification
  • Overview of Certified Kubernetes Administrator (CKA) Certification
  • How to Install and Configure XAMPP on Ubuntu 24 Server (Latest Version โ€“ 2026 Complete Guide)
  • Mastering the Google Cloud Professional DevOps Engineer
  • Mastering Azure Cloud Security: The AZ-500 Path
  • Why AZ-400 is Essential for Global Cloud Engineering Roles
  • Webp format is not supported by PHP installation.
  • Reconfigure PHP 8.2.12 for XAMPP WITH WebP
  • How to Fix โ€œWebP Format is Not Supported by PHP Installationโ€ in XAMPP/LAMPP (Complete 2026 Guide)
  • Fixing WebP Format Is Not Supported by PHP Installation in XAMPP (Ubuntu 24) โ€“ Complete Step-by-Step Guide
  • Azure Solutions Architect Advice for Senior Leads

Recent Comments

  1. digital banking on Complete Tutorial: Setting Up Laravel Telescope Correctly (Windows + XAMPP + Custom Domain)
  2. SAHIL DHINGRA on How to Uninstall Xampp from your machine when it is not visible in Control panel programs & Feature ?
  3. Abhishek on MySQL: List of Comprehensive List of approach to secure MySQL servers.
  4. Kristina on Best practices to followed in .httacess to avoid DDOS attack?
  5. Roshan Jha on Git all Commands

Archives

  • March 2026
  • February 2026
  • January 2026
  • December 2025
  • November 2025
  • October 2025
  • September 2025
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022

Categories

  • Ai
  • AI Blogging
  • AiOps
  • ajax
  • Android Studio
  • Antimalware
  • Antivirus
  • Apache
  • Api
  • API Security
  • Api Testing
  • APK
  • Aws
  • Bike Rental Services
  • ChatGPT
  • Code Linting
  • Composer
  • cPanel
  • Cyber Threat Intelligence
  • Cybersecurity
  • Data Loss Prevention
  • Database
  • dataops
  • Deception Technology
  • DeepSeek
  • Devops
  • DevSecOps
  • DevTools
  • Digital Asset Management
  • Digital Certificates
  • Docker
  • Drupal
  • emulator
  • Encryption Tools
  • Endpoint Security Tools
  • Error
  • facebook
  • Firewalls
  • Flutter
  • git
  • GITHUB
  • Google Antigravity
  • Google play console
  • Google reCAPTCHA
  • Gradle
  • Guest posting
  • health and fitness
  • IDE
  • Identity and Access Management
  • Incident Response
  • Instagram
  • Intrusion Detection and Prevention Systems
  • jobs
  • Joomla
  • Keycloak
  • Laravel
  • Law News
  • Lawyer Discussion
  • Legal Advice
  • Linkedin
  • Linkedin Api
  • Linux
  • Livewire
  • Medical Tourism
  • MlOps
  • MobaXterm
  • Mobile Device Management
  • Multi-Factor Authentication
  • MySql
  • Network Traffic Analysis tools
  • Paytm
  • Penetration Testing
  • php
  • PHPMyAdmin
  • Pinterest Api
  • Quora
  • SAST
  • SecOps
  • Secure File Transfer Protocol
  • Security Analytics Tools
  • Security Auditing Tools
  • Security Information and Event Management
  • Seo
  • Server Management Tools
  • Single Sign-On
  • Site Reliability Engineering
  • soft 404
  • software
  • SuiteCRM
  • SysOps
  • Threat Model
  • Twitter
  • Twitter Api
  • ubuntu
  • Uncategorized
  • Virtual Host
  • Virtual Private Networks
  • VPNs
  • Vulnerability Assessment Tools
  • Web Application Firewalls
  • Windows Processor
  • Wordpress
  • WSL (Windows Subsystem for Linux)
  • X.com
  • Xampp
  • Youtube
©2026 DevSecOps Now!!! | WordPress Theme: EcoCoded
wpDiscuz