我一直在挖掘,以便为这个问题找到解决方案。我终于找到了这篇文章:https : //blog.softwaremill.com/who-am-i-keycloak-impersonation-api-bfe7acaf051a。
总之,在我的情况下,我做了:
在 Keycloak 中创建服务客户端(启用直接访问授权;启用服务帐户):向客户端添加模拟角色;
调用REST API生成中间令牌;
# Getting intermediate token
curl --location --request POST 'http://localhost:8080/auth/realms/MY_REALM/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=vanilla' \
--data-urlencode 'client_secret=c41da386-b4fc-4202-b799-53196284e44f' \
--data-urlencode 'grant_type=client_credentials'
调用 REST API 将令牌交换为模拟访问令牌;
Impersonated user Access-Token
curl --location --request POST 'http://localhost:8080/auth/realms/MY_REALM/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=vanilla' \
--data-urlencode 'client_secret=c41da386-b4fc-4202-b799-53196284e44f' \
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
--data-urlencode 'subject_token=<ACCESS_TOKEN_FROM_PREVIOUS_STEP_2> \
--data-urlencode 'requested_token_type=urn:ietf:params:oauth:token-type:access_token' \
--data-urlencode 'requested_subject=34307875-39a6-4828-8cf0-f59f403bd51f'
使用令牌访问所需的资源;例如
curl --location --request GET 'http://localhost:8090/todos' \
--header 'Authorization: Bearer <ACCESS_TOKEN_FROM_PREVIOUS_STEP_3>'
注意:必须为交换令牌启用 Keycloak 服务器:未实现 Keycloak 模拟 API
ITMISS
相关分类