首页 > 解决方案 > 在模拟 Keycloak 期间在目标客户端的资源上获取 403

问题描述

我正在尝试user通过使用另一个用户(超级管理员)来模拟具有角色(tony123)的 Keycloak 用户。

超级管理员具有分配给客户的impersonation角色realm-management在此处输入图像描述

我还可以在源客户端(源客户端)中为超级管理员生成令牌

curl --location --request POST 'http://localhost:8180/auth/realms/mytenant/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'username=superadmin' \
--data-urlencode 'password=c3VwZXJhZG1pbg==' \
--data-urlencode 'client_id=source-client' \
--data-urlencode 'client_secret=source-client-secret'

我还按照以下步骤将内部令牌设置为内部令牌客户端令牌交换。

  1. 使用命令行参数运行 Keycloak-Dkeycloak.profile.feature.admin_fine_grained_authz=enabled -Dkeycloak.profile.feature.token_exchange=enabled
  2. 在目标客户端中设置令牌交换权限。添加一个基于客户端的策略,并将其source-client作为客户端。
  3. Users在选项卡 上启用权限在此处输入图像描述
  4. 设置admin-impersonating.permission.users权限。添加了source-client作为客户端的基于客户端的策略。

通过上述设置,我可以使用以下 cURL 命令生成模拟令牌

curl --location --request POST 'http://localhost:8180/auth/realms/mytenant/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=source-client' \
--data-urlencode 'client_secret=source-client-secret' \
--data-urlencode 'grant_type=urn:ietf:params:oauth:grant-type:token-exchange' \
--data-urlencode 'subject_token=.....' \
--data-urlencode 'requested_token_type=urn:ietf:params:oauth:token-type:access_token' \
--data-urlencode 'audience=target-client' \
--data-urlencode 'requested_subject=<user id of tony123>'   

使用我从上面的 cURL 命令获得的 access_token,我现在正在尝试访问能够直接访问的target-client资源tony123。当我这样做时,我在通话中被禁止 403。我不确定我在这里缺少什么。提前致谢

标签: javaspring-bootkeycloakopenid-connectimpersonation

解决方案


推荐阅读