简体   繁体   English

使用 Keycloak Java 客户端创建用户时遇到问题

[英]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:您将需要:

  1. Get access_token using admin credentials使用管理员凭据获取access_token
  2. Create a user using that 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.

相关问题 在Openfire中创建用户时遇到问题 - Facing issue while creating user in openfire 使用java将客户端角色添加到keycloak用户 - Add a client role to a keycloak user using java 在 Java 中使用 Spark-BigQuery-Connector 时遇到问题 - Facing Issue while using Spark-BigQuery-Connector with Java 通过HTTP客户端编写curl等效Java代码时遇到的问题 - Facing issue while writing curl equivalent java code through HTTP Client Java Array List - 从用户获取值时遇到问题,但程序在不知不觉中终止 - Java Array List - Facing issue while taking values from user, but unknowingly program terminating 在 Keycloak 中创建新用户期间未分配客户端角色 - client roles haven`t assigned during creating new user in Keycloak 我在Java中使用stringbuilder csv创建CSV文件时遇到问题 - I am facing an issue in creating CSV File using stringbuilder csv in java 使用 keycloak java 库设置 keycloak 用户语言环境 - Setting keycloak user locale using keycloak java library 使用 JDBC TEMPLATE 从 Java 调用 oracle 存储过程时出现问题 - Issue while facing calling oracle stored procedure from Java using JDBC TEMPLATE 使用Stormpath Java API创建客户端时出错 - Error while creating a client using Stormpath Java API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM