json 如何通过keycloak admin rest API通过execute-actions-email更新密码

2ekbmq32  于 2023-10-21  发布在  其他
关注(0)|答案(3)|浏览(122)

我尝试在keycloack中触发密码重置过程,这样用户就会收到一封电子邮件来设置新密码。我总是得到400个回复,
com.fasterxml.jackson.databind.JsonMappingException:无法从[Source:源码:jQuery]的START_OBLOG标记中对java.util.ArrayList示例进行编译io.undertow.servlet.spec.ServletInputStreamImpl@89719e69;行:1,列:第1页]
我在描述的API上调用keycloak:“PUT /admin/realms/{realm}/users/{id}/execute-actions-email”,对象如下:

{"actions":["UPDATE_PASSWORD"]}

参见:http://www.keycloak.org/docs/rest-api/index.html#_send_a_update_account_email_to_the_user

7z5jn7bk

7z5jn7bk1#

解决方案:仅使用["UPDATE_PASSWORD"]作为请求的主体,它可以正常工作。
Java中的一个:Entity.json("[\"UPDATE_PASSWORD\"]");

fdbelqdn

fdbelqdn2#

对于那些想要为execute-actions-email编写REST调用的人,这里是工作代码片段-

CloseableHttpClient httpclient = HttpClients.createDefault();
AccessTokenResponse accessTokenResponse = authzClient.obtainAccessToken(adminUserName, adminPassword);
String urlResetPassword = keyCloakURL+"/admin/realms/"+realmName+"/users/"+userId+"/execute-actions-email";
HttpPut putRequest = new HttpPut(urlResetPassword);
accessTokenResponse = authzClient.obtainAccessToken(adminUserName, adminPassword);
putRequest.addHeader("Authorization", "bearer "+accessTokenResponse.getToken());
putRequest.addHeader("content-type", MediaType.APPLICATION_JSON);
putRequest.setHeader("Accept", MediaType.APPLICATION_JSON);
StringEntity jSonEntity = new StringEntity("[\"UPDATE_PASSWORD\"]");
putRequest.setEntity(jSonEntity);
CloseableHttpResponse response = httpclient.execute(putRequest);

谢谢.

68bkxrlz

68bkxrlz3#

正确的请求将如下。

URL:http://{{HOSTNAME}}:{{PORT}}/auth/realms/{{REALM_NAME}}/users/{{USER_ID}}/execute-actions-email
HTTP-METHOD: PUT
BODY: ["UPDATE_PASSWORD"]
HEADERS: AUTHORIZATION BEARER {{TOKEN-GOES-HERE}}

相关问题