簡體   English   中英

使用 Keycloak Java 客戶端創建用戶時遇到問題

[英]Facing issue while creating user using Keycloak Java client

這是我試圖在master領域中創建 Keycloak 用戶的簡單代碼。 但是我收到了Bad request作為回報,並且在Keycloak低於警告:

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);

使用 Keycloak REST API 需要access_token 因此,您需要檢查您正在使用的客戶端是否Direct Access Grants Enabled: ON 它默認為客戶端admin-cli啟用。 但是,看起來它被security-admin-console禁用。

我認為最好使用admin-cli

例如,還嘗試通過 Postman 使用 HTTP 請求來執行相同的操作。 您將需要:

  1. 使用管理員憑據獲取access_token
  2. 使用該令牌創建一個用戶。

您可以在此處找到使用 Admin REST API 的完整示例: https : //github.com/v-ladyev/keycloak-nodejs-example

此示例使用 Node.js。 但是您可以分析代碼以更好地了解如何使用 REST API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM