我刚刚开始使用 keycloak,似乎遇到了障碍。我无法使用 java 的 keycloak-admin-client 以编程方式创建用户。我的keycloak服务器版本是8.0.0,库/jar版本也相同。当我尝试创建用户时,我的程序只是坐在那里继续等待,就像在日志中我可以看到登录错误
2019-12-03 20:10:19,842 WARN [org.keycloak.events] (default task-26) type=LOGIN_ERROR, realmId=root-admin, clientId=demo-app, userId=null, ipAddress=192.x.x.x, error=not_allowed, auth_method=oauth_credentials, grant_type=password, client_auth_method=client-secret
Keycloak keycloak = KeycloakBuilder.builder() //
.serverUrl("http://192.x.x.x:8080/auth") //
.realm("root-admin") //
.grantType(OAuth2Constants.PASSWORD) //
.clientId("demo-app")
.clientSecret("")////
.username("genghis khan") //
.password("1234") //
.build();
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue("12345678");
UserRepresentation user = new UserRepresentation();
user.setEnabled(true);
user.setUsername("michaeljackson");
user.setFirstName("michael");
user.setLastName("jackson");
user.setCredentials(Arrays.asList(credential));
Response resp=keycloak.realm("root-admin").users().create(user);
System.out.println(resp.getStatus());
我的客户端是一个面向公众的应用程序,因此它的访问类型设置为公共(public),因此不会为其生成客户端 key ,而且 genghis khan 用户具有管理员角色,并且 root-admin 领域已由我创建。需要有关如何完成这项工作的帮助
最佳答案
尝试在管理控制台上启用 demo-app
客户端的“Direct Access Grants Enabled”。
.grantType(OAuth2Constants.PASSWORD) //
此行表示您正在使用“资源所有者密码凭据授予”,并且需要启用它。
另请参阅: https://www.keycloak.org/docs/8.0/server_admin/index.html#oidc-clients
关于java - 使用 keycloak 管理客户端以编程方式为具有 access-type public 的客户端创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59160385/