Pro*_*mer 9 java keycloak keycloak-services
我正在尝试以编程方式在 keycloak 中访问创建用户。但是我收到 403 作为状态代码。我正在关注以下链接。
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);
Run Code Online (Sandbox Code Playgroud)
response.status 是 403
我在使用 KeyCloak 9.0.3 时遇到了同样的问题。最终对我有用的是:
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"
Run Code Online (Sandbox Code Playgroud)
我不知道为什么,但在主领域上使用管理客户端对我来说根本不起作用。
使用上面正确答案中提到的角色在目标领域中设置 admin-cli 客户端。
除了将新角色添加到范围之外,我还必须将该角色添加到服务帐户角色。
顺便说一句,Keycloak 9.0.3 的 Admin Rest API 文档中甚至没有提到客户端凭据访问令牌作为选项,但它确实有效。
| 归档时间: |
|
| 查看次数: |
10581 次 |
| 最近记录: |