gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:53:48 26 4
gpt4 key购买 nike

尝试使用 Keycloak 作为身份提供者。我正在使用 ./standalone.sh 脚本运行它。

所以,我像这样获取access_token:

curl --request POST \
--url http://localhost:8080/auth/realms/master/protocol/openid-connect/token \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data grant_type=client_credentials \
--data client_id=admin-cli \
--data client_secret=<the-client-secret-under-master-realm-for-admin-cli-client>

响应:

{
"access_token": "the-access-token",
"expires_in": 60,
"refresh_expires_in": 0,
"token_type": "Bearer",
"not-before-policy": 0,
"scope": "profile email"
}

然后很快,在我的 test-realm 下,我尝试创建一个用户,如下所示:

curl --request POST \
--url http://localhost:8080/auth/admin/realms/test-realm/users \
--header 'Authorization: Bearer the-access-token' \
--header 'Content-Type: application/json' \
--data '{
"firstName": "Sergey",
"lastName": "Kargopolov",
"email": "test@test.com",
"enabled": "true",
"username": "app-user"
}'

然后我遇到了 403:

< HTTP/1.1 403 Forbidden
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Referrer-Policy: no-referrer
< Date: Thu, 28 Jan 2021 23:43:57 GMT
< Connection: keep-alive
< Strict-Transport-Security: max-age=31536000; includeSubDomains
< X-Content-Type-Options: nosniff
< Content-Type: application/json
< Content-Length: 25

有什么我想念的吗?我正在关注 this tutorial我完全按照描述做所有事情!

编辑:我尝试了密码授予方式来获取不记名 token 并且有效,但不是客户端 secret 方式。我显然更喜欢客户端 secret 方式(这是我目前遇到的问题)。这可能是什么问题?

最佳答案

要使用 Keycloak Rest API 创建用户,只需要从 admin-cli 客户端请求一个代表管理员用户的 token ,提供其名称和密码,例如如下:

TOKEN=$(curl -k -sS     -d "client_id=admin-cli" \
-d "username=$ADMIN_NAME" \
-d "password=$ADMIN_PASSWORD" \
-d "grant_type=password" \
http://$KEYCLOAK_IP/auth/realms/master/protocol/openid-connect/token)

从 $TOKEN 对象中提取访问 token (让我们命名为 $ACCESS_TOKEN)。

然后创建用户如下:

curl -k -sS -X POST https://$KEYCLOAK_IP/auth/admin/realms/$REALM_NAME/users \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d "$USER_JSON_DATA"

$USER_JSON_DATA 将是要创建的用户的 json 数据表示。默认情况下,使用 Keycloak 部署的 master 管理员无需添加 admin 角色。

如果正常设置,您只需要知道(正如我已经描述的那样)admin 的 名称和密码,这是在初始设置中配置的。

如果您单击 admin 用户 > 角色,您将看到以下内容:

enter image description here

admin 用户已经拥有 admin 角色。

Edit: I tried the Password Grant way to obtain the Bearer Token andthat worked, but NOT the client secret way. I obviously prefer theclient secret way (which is where I'm stuck currently). What could bethe issue here?

现在,如果您完全按照原来的方式更改 admin_cli 配置,则需要向 Service-account-admin-cli 用户添加管理员角色。

现在的问题是 Service-account-admin-cli 用户是 hidden User 部分。尽管如此,您还是可以执行以下操作:

  1. 使用您的设置再次请求管理员 token ;
  2. 转到 Master Realm > Clients > admin-cli > Session > 单击 [Show Session]:

enter image description here

  1. 点击用户 service-account-admin-cli;
  2. 转到角色映射;
  3. 分配 admin 角色;

enter image description here

由于 service-account-admin-cli 用户现在具有 admin 角色,代表该用户的 token 请求将包含创建用户所需的权限。

如果上述方法不起作用,请执行以下操作:

  • 领域大师;
  • 客户端 > admin-cli;
  • 转到映射器;
  • 点击【创建】;
  • 作为映射器类型选择“硬编码角色”;
  • 点击“选择角色”并选择“管理员”;
  • 点击[保存]。

关于用于创建用户的 Keycloak API 返回 403 Forbidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65946850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com