[英]Facing issue while creating user using Keycloak Java client
This is below simple code I am trying to create Keycloak user in the master
Realm.这是我试图在
master
领域中创建 Keycloak 用户的简单代码。 But I am getting Bad request
in return and below warning in Keycloak
:但是我收到了
Bad request
作为回报,并且在Keycloak
低于警告:
10:27:58,256 WARN [org.keycloak.events] (default task-111) type=LOGIN_ERROR, realmId=master, clientId=security-admin-console, userId=null, ipAddress=127.0.0.1, error=not_allowed, auth_method=oauth_credentials, grant_type=password, client_auth_method=client-secret
10:27:58,256 WARN [org.keycloak.events](默认任务-111)type=LOGIN_ERROR,realmId=master,clientId=security-admin-console,userId=null,ipAddress=127.0.0.1,error=not_allowed,auth_method =oauth_credentials,grant_type=密码,client_auth_method=client-secret
Keycloak kc = Keycloak.getInstance("http://localhost:8080/auth", "master", "admin", "password","security-admin-console");
CredentialRepresentation credential = new CredentialRepresentation();
credential.setType(CredentialRepresentation.PASSWORD);
credential.setValue(password);
credential.setTemporary(false);
UserRepresentation user = new UserRepresentation();
user.setUsername(username);
user.setFirstName("Test");
user.setLastName("User");
user.setEnabled(true);
user.setCredentials(Arrays.asList(credential));
kc.realm("master").users().create(user);
access_token
is needed to use Keycloak REST API.使用 Keycloak REST API 需要
access_token
。 So you need to check that Direct Access Grants Enabled: ON
for the client you are using.因此,您需要检查您正在使用的客户端是否
Direct Access Grants Enabled: ON
。 It is enabled by default for the client admin-cli
.它默认为客户端
admin-cli
启用。 But, looks like, it is disabled for the security-admin-console
.但是,看起来它被
security-admin-console
禁用。
I think better to use admin-cli
.我认为最好使用
admin-cli
。
Also try to do the same using HTTP requests with Postman, for example.例如,还尝试通过 Postman 使用 HTTP 请求来执行相同的操作。 You will need:
您将需要:
access_token
using admin credentialsaccess_token
You can find a full example of working with Admin REST API here: https://github.com/v-ladynev/keycloak-nodejs-example您可以在此处找到使用 Admin REST API 的完整示例: https : //github.com/v-ladyev/keycloak-nodejs-example
This example uses Node.js.此示例使用 Node.js。 But you can analyse the code to understand better how to use REST API.
但是您可以分析代码以更好地了解如何使用 REST API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.