keycloak-rest-api - key 斗篷 API :- Identify Users Realm for login

标签 keycloak-rest-api

我正在创建一个微服务,它将代理 keycloak 来创建用户、重置密码、登录等。我不想暴露任何 keycloak 页面,例如重置密码或登录页面,所以我使用 keycloak API,一切都是到目前为止还好。

唯一的问题是登录,我需要知道领域才能获取 token ,因为获取 token 的 API 是特定于领域的。

realms/{REALM_NAME}/protocol/openid-connect/token

那么有没有办法通过管理员用户获取所有领域的所有用户的列表? 或者有其他方法可以找到它吗?

最佳答案

您可以通过解码用户的访问 token 来获取领域信息。

“iss”(发行者)声明标识了发行 JWT 的主体。

这是 JWT.io 的解码示例 enter image description here

我演示了两个领域(领域1和领域2) 每个领域添加单个用户(两个用户相同的用户名:用户和密码:1234) enter image description here

enter image description here 并调用获取访问 token 并通过Python对其进行解码

import requests
import ast
import jwt

def get_issuer(realm, user_name, password):
    url = 'http://localhost:8180/auth/realms/'+realm+'/protocol/openid-connect/token'
    body = {
        'client_id': 'admin-cli',
        'grant_type': 'password',
        'username' : user_name,
        'password': password
    }
    headers = {
        'content-type': 'application/x-www-form-urlencoded'
    }

    response = requests.post(url, data=body, headers=headers).content.decode('utf-8')
    token = ast.literal_eval(response)['access_token']
    # print(token)
    decoded = jwt.decode(token, options={"verify_signature": False})
    # print(decoded)
    return decoded['iss']

print('realm1 with user -->', get_issuer('realm1','user','1234'))
print('realm2 with user -->', get_issuer('realm2','user','1234'))

获取此输出

$python get_realm.py
realm1 with user --> http://localhost:8180/auth/realms/realm1
realm2 with user --> http://localhost:8180/auth/realms/realm2

如果你想获取realm的所有用户, 您可以使用主领域的管理 token 获取此 API

GET/{realm}/users

关于keycloak-rest-api - key 斗篷 API :- Identify Users Realm for login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73068333/

相关文章:

keycloak - 哪些角色使 Keycloak 领域中的用户能够使用 Admin-REST-API?

keycloak - 如何使用 API 在 Keycloak 中设置用户属性值?

keycloak - 如何使用客户端在 Keycloak 中发布领域角色?

spring-boot - 是否可以在没有管理员凭据的情况下创建 key 斗篷用户?

Keycloak获取用户密码

node.js - 使用 Keycloak Rest API 将用户添加到客户端角色

KeyCloak - 入口不允许通过 https 服务连接

用于创建用户的 Keycloak API 返回 403 Forbidden