无法在密钥斗篷中创建用户。获取 403 状态

cannot create user in the keycloak. Getting 403 status

我正在尝试以编程方式访问在 keycloak 中创建用户。但是我收到 403 作为状态代码。我正在关注以下 link.

https://technology.first8.nl/programmatically-adding-users-in-keycloak/

谁能帮帮我?提前致谢

我使用以下代码创建用户

Keycloak kc = Keycloak.getInstance(
                     "http://{server name}:8080/auth",
                     "{realm name}", // the realm to log in to
                     "{useraname}", 
                     "{password}",  // the user
                     "{client id}",
                     "{client secret key}");

            CredentialRepresentation credential = new CredentialRepresentation();
            credential.setType(CredentialRepresentation.PASSWORD);
            credential.setValue("test123");
            UserRepresentation user = new UserRepresentation();
            user.setUsername("codeuser");
            user.setFirstName("sampleuser1");
            user.setLastName("password");

            user.setCredentials(Arrays.asList(credential));
            user.setEnabled(true);
            Response result = kc.realm("{realm name}").users().create(user);

response.status 将以 403

的形式出现

如果您没有使用来自 master 领域的 admin 用户。

您需要为您用来获取 Keycloak 实例的用户分配适当的角色,例如来自 realm-management 客户端的 manage-users 角色。

我遇到了同样的问题。我就是这样修复它的。

  1. 创建至少具有 manage-users 领域管理角色的角色

UI 服务器 9.0.2 更新

  1. 转到您客户的 Scope 选项卡并将角色添加到您的 Realm Roles

我在使用 KeyCloak 9.0.3 时遇到了同样的问题。 最终对我有用的是:

  1. 在目标领域(我尝试创建用户的地方)使用带有客户端凭据的 admin-cli 客户端
curl \
  -d "client_id=admin-cli\
  -d "client_secret=<YOUR_CLIENT_SECRET>" \
  -d "grant_type=client_credentials" \
  "http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token"

我不确定为什么,但在主域上使用管理客户端对我来说根本不起作用。

  1. 使用上面正确答案中提到的角色在目标领域中设置 admin-cli 客户端。

  2. 除了将新角色添加到 Scope 之外,我还必须将角色添加到 Service Account Roles。

顺便说一下,在 Keycloak 9.0.3 的 Admin Rest API 文档中甚至没有提到客户端凭据访问令牌作为一个选项,但它确实有效。

如果使用Service Accounts认证,需要在tab中添加preced answer角色Service Accounts Roles

喜欢下面