簡體   English   中英

鑰匙斗篷 Java。 嘗試使用設置的密碼創建一個新用戶

[英]Keycloak Java. Try to create a new user with set password

鑰匙斗篷 11.0。 我正在嘗試通過 Keycloak 服務器的管理員 REST 端點在 Keycloak 中創建一個新用戶。 我在 Keycloak 服務器上面臨堆棧跟蹤,如下所示。

0 8:59:40,744 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-4) Uncaught server error: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<org.keycloak.representations.idm.CredentialRepresentation>` out of VALUE_STRING token
at [Source: (io.undertow.servlet.spec.ServletInputStreamImpl); line: 1, column: 141] (through reference chain: org.keycloak.representations.idm.UserRepresentation["credentials"])

我的代碼如下。

private void sendCreateUserRequest(UserEntity userEntity, KeycloakTokenModel keycloakTokenModel) throws JsonProcessingException {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(keycloakTokenModel.getAccessToken());
        JsonObject properties = new JsonObject();
        properties.addProperty(FIRST_NAME, userEntity.getFirstName());
        properties.addProperty(LAST_NAME, userEntity.getLastName());
        properties.addProperty(EMAIL, userEntity.getEmail());
        properties.addProperty(ENABLED, DEFAULT_ENABLED_OPTION);
        properties.addProperty(USERNAME, userEntity.getEmail());
        String credentialsArray = mapToCredentialsArray(userEntity.getPassword());
        properties.addProperty(CREDENTIALS, credentialsArray);
        String propertiesString = properties.toString();
        HttpEntity<String> request = new HttpEntity<>(propertiesString, headers);
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.postForObject(userEndpoint, request, String.class);
    }

在調試器中我看到

propertiesString = 
{
"firstName":null,
"lastName":null,
"email":"Tester_Testerov@some.com",
"enabled":"true",
"username":"Tester_Testerov@some.com",
"credentials":
"[
 {\"value\":\"verystrongpassword4\",\"type\":\"password\",\"temporary\":false}
]"
}

但是沒有那行 String credentialsArray = mapToCredentialsArray(userEntity.getPassword()); 它有效,但我想在此請求中為用戶設置憑據。 請幫忙

它只是格式錯誤的 json。 看到您的數組以"開頭和結尾,這是 json arrays 的無效語法。我認為properties.addProperty(CREDENTIALS, credentialsArray);"字符轉義您的數組。

暫無
暫無

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

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