简体   繁体   English

Keycloak REST API 无法从用户角色映射中删除客户端级角色

[英]Keycloak REST API Unable to Delete client-level roles from user role mapping

Hoping someone can help me on this.希望有人可以帮助我。 I've created my own Keycloak Realm, and client.我创建了自己的 Keycloak Realm 和客户端。 I am using Spring boot and KeycloakRestTemplate from org.keycloak.adapters.springsecurity.client.KeycloakRestTemplate;我正在使用来自 org.keycloak.adapters.springsecurity.client.KeycloakRestTemplate 的 Spring boot 和 KeycloakRestTemplate; to make all my calls.拨打我所有的电话。

I've been successful in adding client-level roles to the user role mapping to any given user.我已经成功地将客户端级角色添加到任何给定用户的用户角色映射中。

I prefix my URI with /admin/realms/ when using the Keycloak API docs.使用 Keycloak API 文档时,我在 URI 前面加上 /admin/realms/。 So far all my requests have worked (getting a list of users from my client, getting a list of users that have a particular client-level role, and even adding client-level roles to a user as described above)到目前为止,我的所有请求都有效(从我的客户端获取用户列表,获取具有特定客户端级角色的用户列表,甚至如上所述向用户添加客户端级角色)

My problem is I cannot delete client-level roles from a user.我的问题是我无法从用户中删除客户端级角色。 I've looked at the keycloak docs and it looks like I've followed everything correctly.我查看了 keycloak 文档,看起来我已经正确地遵循了所有内容。 I also made sure the user had applicable client roles available to be deleted.我还确保用户具有可删除的适用客户角色。 I really appreciate any comments or help given!!我真的很感谢任何意见或帮助!

https://www.keycloak.org/docs-api/14.0/rest-api/index.html https://www.keycloak.org/docs-api/14.0/rest-api/index.html

"Delete client-level roles from user role mapping DELETE /{realm}/users/{id}/role-mappings/clients/{client}" “从用户角色映射删除客户端级角色 DELETE /{realm}/users/{id}/role-mappings/clients/{client}”

 import org.keycloak.adapters.springsecurity.client.KeycloakRestTemplate; . . . @Autowired private KeycloakRestTemplate restTemplate; . . . . . UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(keycloakServerUrl + "/admin/realms/"+keycloakRealm+"/users/"+userId+"/role-mappings/clients/"+keycloakClientId); this.restTemplate.postForEntity(builder.toUriString(), rolesList, List.class); // this works! Note: rolesList is an List<RoleRepresentation> object . . . this.restTemplate.delete(builder.toUriString(), rolesList); // Does not work!

URI: http://XXXXXXXXXXXXXXX:8180/auth/admin/realms/VLS/users/2144cc43-59f4-4406-9527-2a59ee0c3751/role-mappings/clients/53e659e1-7cef-4dbb-8cdd-b786ca3a44a4 URI:http://XXXXXXXXXXXXXXX:8180/auth/admin/realms/VLS/users/2144cc43-59f4-4406-9527-2a59ee0c3751/role-mappings/clients/53e659e1-7cef-4dbb-8cdd-b786ca3a44a4

Error when calling Delete API: org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 Unsupported Media Type: [{"error":"RESTEASY003065: Cannot consume content type"}]调用删除 API 时出错:org.springframework.web.client.HttpClientErrorException$UnsupportedMediaType: 415 Unsupported Media Type: [{"error":"RESTEASY003065: Cannot consume content type"}]

Edit 1: I have also given myself ALL available roles from all clients as a precaution beforehand.编辑1:作为预防措施,我还为自己提供了所有客户的所有可用角色。 I understand some roles are needed to perform certain tasks even through the API.我知道即使通过 API 也需要一些角色来执行某些任务。 I've taken this into account.我已经考虑到了这一点。

KeycloakRestTemplate appears to inherit all of its methods directly from Spring's RestTemplate . KeycloakRestTemplate 似乎直接从 Spring 的RestTemplate 继承了它的所有方法。 According to the documentation for that class, the second argument to delete isn't a request body, as I think you're intending.根据该类的文档delete的第二个参数不是请求正文,正如我认为您的意图。 Rather, it's a vararg of objects used to expand template variables in the URI.相反,它是用于扩展 URI 中的模板变量的对象的可变参数。

There doesn't appear to be a variant of the delete method that allows you to supply a body, so you'll probably need to use one of the variants of the execute or exchange methods that accepts an HTTP method and a request entity instead.似乎没有允许您提供正文的delete方法的变体,因此您可能需要使用接受 HTTP 方法和请求实体的executeexchange方法的变体之一。 In fact, the RestTemplate API makes this quite difficult to do, because it's generally assumed that DELETE requests don't have bodies.事实上, RestTemplate API 使这很难做到,因为通常假设DELETE请求没有主体。

The alternate way to interact with Keycloak can be using the keycloak starter dependency and the keycloak admin client .与 Keycloak 交互的另一种方法是使用keycloak starter 依赖项和keycloak 管理客户端

  1. Add the dependencies mentioned above.添加上面提到的依赖项。

  2. Configure the Keycloak admin user.配置 Keycloak 管理员用户。

     public Keycloak getAdminKeycloakUser() { return KeycloakBuilder.builder() .serverUrl(keycloakAuthUrl) .grantType(OAuth2Constants.PASSWORD) .realm(masterRealm).clientId(masterClient) .username(adminUsername).password(adminPassword) .resteasyClient(new ResteasyClientBuilder().connectionPoolSize(10).build()) .build(); }

With this admin user we can delete the client user along with other actions.使用此管理员用户,我们可以删除客户端用户以及其他操作。

getAdminKeycloakUser().realm(realm).clients().roles().deleteRole(roleName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM