英文:
Keycloak Java. Try to create a new user with set password
问题
Keycloak 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());
,它可以工作,但我想在此请求中设置用户的凭据。请帮助。
英文:
Keycloak 11.0.
I am trying to create a new user in Keycloak via admin REST endpoint of Keycloak server.
I am facing stack trace on Keycloak server as below.
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"])
My code is as below.
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);
}
In debugger I see
propertiesString =
{
"firstName":null,
"lastName":null,
"email":"Tester_Testerov@some.com",
"enabled":"true",
"username":"Tester_Testerov@some.com",
"credentials":
"[
{\"value\":\"verystrongpassword4\",\"type\":\"password\",\"temporary\":false}
]"
}
But without that line String credentialsArray = mapToCredentialsArray(userEntity.getPassword()); it works but I want to set credential to user in this request. Please, help
答案1
得分: 0
只是格式错误的 JSON。请注意,您的数组以 "
开始和结束,这对于 JSON 数组来说是无效的语法。我认为 properties.addProperty(CREDENTIALS, credentialsArray);
会使用 "
字符转义您的数组。
英文:
It is just bad formatted json. See that your array starts and ends with "
and this is invalid syntax for json arrays. I think properties.addProperty(CREDENTIALS, credentialsArray);
escapes your array with "
character.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论