Skip to content

Keycloak Single Sign-On Example (SAML)


Keycloak is an open-source identity authentication and access control solution for modern applications and distributed services.

This article uses a deployed Keycloak server to demonstrate how to implement SSO login for Keycloak users to the Guance management console using the SAML 2.0 protocol.

For information about implementing SSO login for Keycloak users to the Guance management console using the OpenID Connect protocol, see Keycloak Single Sign-On (Deployment Plan).

Prerequisites

Prepare Keycloak accessible from a browser, permission to administer the target realm, and SSO management permission for the target workspace. This example follows the Keycloak 26 Admin Console and uses the realm company and the test email alice@example.com.

Concepts

The following are explanations of basic concepts in the Keycloak configuration process:

Field Description
Realm Realm, similar to a workspace, is used to manage users, credentials, roles, and user groups. Realms are isolated from each other.
Clients Clients are applications or services that can request Keycloak to authenticate users.
Users User accounts that can log in to the system. You need to configure the login email and Credentials.
Credentials Credentials used to verify user identity. They can be used to set the login password for a user account.
Authentication The process of identifying and verifying a user.
Authorization The process of granting users access permissions.
Roles Used to identify the user's identity type, such as administrator or regular user.
User role mapping The mapping relationship between users and roles. One user can be associated with multiple roles.
Groups Manage user groups. Supports mapping roles to groups.

Steps

1. Select or create a Keycloak realm

Go to the Keycloak Admin Console and select the realm that manages enterprise users from the realm drop-down menu in the upper-left corner. If no realm is available, click Create realm, set Realm name to company, and click Create. Subsequent clients and users are configured under this realm.

2. Obtain Keycloak IdP Metadata

  1. Open the SAML metadata URL for the target realm in a browser: https://<keycloak-host>/realms/company/protocol/saml/descriptor. Replace <keycloak-host> and company with the actual domain name and realm name. If the deployment uses a path prefix such as /auth, keep the prefix before /realms/.
  2. Save the complete XML returned as keycloak-idp.xml. Confirm that the XML contains IDPSSODescriptor and a signing certificate. Keep the original SingleSignOnService address in the XML; Guance sends SAML requests to this address. Keycloak SAML metadata documentation

3. Add a SAML Identity Provider in Guance

  1. In the target workspace, go to Manage > Member Management > SSO Management > User SSO, select SAML, and click Add Identity Provider.
  2. In Identity Provider, enter Keycloak; in Metadata Document, upload keycloak-idp.xml.
  3. In Mapping Configuration, set both Email and Username to Email, consistent with the assertion attribute name in step 5. Fill in Mobile Number as needed.
  4. In Access Restrictions, enter alice@example.com, select Role Authorization, confirm Session Persistence, and then save. For testing, you can select Read-only Member and use the default duration.
  5. Click Edit for the identity provider, and copy the Entity ID and Assertion URL from Service Provider Details respectively. If you need to provide an entry point on the portal, also copy the Login URL.

4. Create and Configure a SAML Client in Keycloak

  1. Return to the same realm and go to Clients > Create client. Set Client type to SAML, and set Client ID to the Entity ID copied in step 3. Click Next. If a client with the same Client ID already exists, open the existing client and verify its configuration.
  2. In the client's Settings, fill in and save the fields as shown in the following table. Some fields also appear in the creation wizard.

    Keycloak Field Value
    Client ID The Entity ID of Guance
    Valid Redirect URIs Add the complete Assertion URL of Guance
    Master SAML Processing URL The Assertion URL of Guance. If Assertion Consumer Service POST Binding URL is configured separately via Advanced > Fine Grain SAML Endpoint Configuration, enter the same value.
    Force POST Binding On, to return SAML responses via HTTP POST
    Sign Assertions On, to sign SAML assertions
  3. Open the client's Keys, and set Client signature required to off. Authentication requests sent by Guance use an unsigned configuration, and returned assertions are signed by Keycloak. Keycloak SAML client configuration

5. Configure Email Attribute Mapping

  1. In this client, go to Client scopes, and open the dedicated scope labeled Dedicated scope and mappers for this client.
  2. In Mappers, click Add mapper > By configuration. When adding for the first time, it may also be displayed as Configure a new mapper. Select User Property. In older console versions, you can add it directly from the client's Mappers page.
  3. Fill in and save as shown in the following table. Keycloak User Property mapper

    Field Value
    Name guance-email
    Property email
    SAML Attribute Name Email
    SAML Attribute NameFormat Basic

6. Configure a Keycloak Test User

  1. In the current realm, go to Users. If an employee account already exists, open the account and confirm that Email is alice@example.com. To create a test user, click Add user, enter Username and Email, keep Enabled on, and then save.
  2. If a new user needs login credentials, go to Credentials in the user details, click Set password, and set a password. Users with a temporary password are prompted to change the password on first login.
  3. In the SAML list of Guance, confirm that Keycloak is enabled.

7. Verify Login

  1. Open the Guance login page for the site in an incognito window, click Single Sign-On, enter alice@example.com, and click Get Login URL.
  2. Select Keycloak and click Authenticate/Log In. On the Keycloak login page, complete authentication using the test account from step 6.
  3. After returning to Guance, go to the target workspace and verify the role. If Keycloak reports that the client does not exist, verify the Client ID. If it reports that the redirect URL is invalid, verify the Valid Redirect URIs and Master SAML Processing URL. If it reports that the email was not found after returning, verify the user's Email and the mapper's SAML Attribute Name. Login Troubleshooting

If you need to initiate login from Keycloak, set IDP-Initiated SSO URL name in the client's Settings, for example guance. Use https://<keycloak-host>/realms/company/protocol/saml/clients/guance to verify this entry point; if the deployment has a path prefix, keep that prefix. Keycloak-initiated SAML login documentation

Feedback

Is this page helpful?