使用java将客户端角色添加到keycloak用户

时间:2018-03-05 12:13:05

标签: java admin resteasy roles keycloak

我正在使用Keycloak Admin Client库尝试创建用户,然后将该客户端角色添加到该创建的用户。我正在创建没有问题的用户,但是当我尝试为该用户分配角色时,我收到错误:

  <option value="75">item-127 ( 120127 )</option>
  <option value="74">item-163 ( 0 )</option>
  <option value="94">item-139 ( 0 )</option>
  <option value="89">item-135 ( 0 )</option>
  <option value="369">Item-AC95 ( AC95 )</option>
  <option value="63">item-159 ( 120159 )</option>

这是我破解的代码:

javax.servlet.ServletException: java.lang.IllegalStateException: RESTEASY003765: Response is closed.
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:487)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:425)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:383)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:336)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:223)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.apache.catalina.filters.CorsFilter.handleNonCORS(CorsFilter.java:441)
    org.apache.catalina.filters.CorsFilter.doFilter(CorsFilter.java:169)

这是我的pom.xml:

// Get realm
        RealmResource realmResource = keycloak.realm(realm);
        UsersResource userRessource = realmResource.users();

                System.out.println(userId);

        // Get client
        ClientRepresentation app1Client = realmResource.clients()
                .findByClientId("digicor").get(0);

                System.out.println("app1Client.getId: "+app1Client.getId());

                System.out.println("realmResource.clients()) = "+ realmResource.clients().toString());

                System.out.println("realmResource.roles().list() ="+realmResource.roles().list());

                System.out.println("realmResource.roles().list().get(0) ="+realmResource.roles().list().get(0));

                System.out.println(" realmResource.clients().findAll() ="+ realmResource.clients().findAll());

                System.out.println(" realmResource.clients().findAll().get(0).toString ="+ realmResource.clients().findAll().toString());

                System.out.println("realmResource.clients().get(digicor) = "+ realmResource.clients().get("digicor"));

                System.out.println("realmResource.clients().get(digicor).roles() = "+ realmResource.clients().get("digicor").roles());

                System.out.println("realmResource.clients().get(\"digicor\").roles().get(\"development\") = "+ realmResource.clients().get("digicor").roles().get("development").toString());

                RoleRepresentation clientRole = realmResource.clients().get("digicor").roles().get("development").toRepresentation();
      //Breaks on the above line and never reaches this print.           
                System.out.println("Role Representation made");

我正在使用maven构建此项目,然后将其作为服务器上的docker镜像运行。

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:2)

我以下列方式为用户分配了角色

String userRole = "development";

List<RoleRepresentation> roleRepresentationList = userResource.roles().realmLevel().listAvailable();

for (RoleRepresentation roleRepresentation : roleRepresentationList)
  {
    if (roleRepresentation.getName().equals(userRole))
      {
        userResource.roles().realmLevel().add(Arrays.asList(roleRepresentation));
        break;
     }
  }

如果要将“客户端级别”角色分配给“用户”,请执行以下操作

ClientRepresentation clientRep = realmResource.clients().findByClientId("digicor").get(0);
RoleRepresentation clientRoleRep = realmResource.clients().get(clientRep.getId()).roles().get("development").toRepresentation();
userResource.roles().clientLevel(clientRep.getId()).add(Arrays.asList(clientRoleRep));