متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 209Security · Keycloak

How to set up SSO login with Keycloak

Grafana, Argo CD, the wiki and the admin panel each have their own users and passwords — and when someone leaves, you forget one. Keycloak is one login (single sign-on) for all of them with OpenID Connect: users and groups in one place, MFA for everybody, and one switch to disable a person. This how-to creates a realm, an OIDC client for an app (Grafana as the example), a user with MFA, and connects the app.

  • 5 الدروس
  • 4 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

209.1

The quick answer

KC=/opt/keycloak/bin/kcadm.sh
$KC config credentials --server https://sso.example.com --realm master --user admin
$KC create realms -s realm=company -s enabled=true
$KC create clients -r company -s clientId=grafana -s publicClient=false -s standardFlowEnabled=true -s 'redirectUris=["https://grafana.example.com/login/generic_oauth"]'
$KC create users -r company -s username=alice -s email=alice@example.com -s enabled=true -s 'requiredActions=["CONFIGURE_TOTP"]'
$KC set-password -r company --username alice --new-password 'Temp-Password-123' --temporary

209.2

Step by step

  1. Own realm — keep master for Keycloak admins only; users and apps go into a realm like company.
  2. OIDC client per app (quick answer, line 4) — confidential (publicClient=false), authorization code flow (standardFlowEnabled), and the exact redirect URI of the app.
  3. Get the client secret for the app's config:
CID=$($KC get clients -r company -q clientId=grafana --fields id --format csv --noquotes)
$KC get clients/$CID/client-secret -r company
  1. Users and groups — create users (line 5) or connect LDAP/Active Directory (User federation). Put people in groups (grafana-admins, grafana-viewers) and add a groups mapper to the client so the token contains them.
  2. MFA — CONFIGURE_TOTP makes the user set up an authenticator app at first login; make OTP required for the whole realm in the authentication flow.
  3. Connect the app — every OIDC app needs the same facts, found at https://sso.example.com/realms/company/.well-known/openid-configuration. For Grafana:
/etc/grafana/grafana.ini (part)
[auth.generic_oauth]
enabled = true
name = Company SSO
client_id = grafana
client_secret = $__file{/etc/grafana/keycloak-secret}
scopes = openid email profile
auth_url = https://sso.example.com/realms/company/protocol/openid-connect/auth
token_url = https://sso.example.com/realms/company/protocol/openid-connect/token
api_url = https://sso.example.com/realms/company/protocol/openid-connect/userinfo
role_attribute_path = contains(groups[*], 'grafana-admins') && 'Admin' || 'Viewer'
  1. Offboarding — disable the user in Keycloak: all SSO apps refuse new logins at once (existing app sessions end at their next token refresh).
  2. Run Keycloak safely — HTTPS with the right hostname setting, behind a proxy with proxy-headers xforwarded, a real database (PostgreSQL), backups, and the admin console not open to the internet.

الأوامر في هذا الدرس

209.3

Check that it worked

The discovery URL returns JSON with issuer https://sso.example.com/realms/company; Grafana shows "Sign in with Company SSO"; alice logs in, must set a new password and an OTP, and gets the role from her group; after disabling alice in Keycloak she cannot log in again.

209.4

Common problems

ProblemCauseFix
Invalid parameter: redirect_uriRedirect URI in the client differs from the app's.Copy the exact URL (scheme, host, path); no trailing slash differences.
Login loops or invalid issuerKeycloak builds URLs with the wrong host or http behind a proxy.Set hostname; proxy-headers xforwarded; HTTPS end to end.
App gets no groupsNo groups mapper on the client.Add a "Group Membership" mapper (claim groups, no full path).
invalid_client / unauthorizedWrong client secret or a public client.Copy the secret again; publicClient=false.
Token errors "not active yet / expired"Clock difference between servers.Time sync (chrony) on Keycloak and apps.
Admin console reachable from the internetSame hostname for users and admins.Separate admin hostname, or block /admin at the proxy.

209.5

Go further

1Why create your own realm instead of using master?

The master realm is for administering Keycloak itself; keeping users and apps in their own realm limits the damage of a mistake.

2What happens when you disable a user in Keycloak?

All apps that log in through Keycloak refuse new logins for that user, so one switch removes access everywhere.

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل