Keycloak 管理员客户端

我尝试使用 keycloak 客户端管理员在我的 keycloak 中创建一个用户


<dependency>

  <groupId>org.keycloak</groupId>

  <artifactId>keycloak-admin-client</artifactId>

  <version>6.0.1</version>

</dependency>

并使用此代码:


Keycloak keycloak = KeycloakBuilder.builder() //

                .serverUrl(serverUrl) //

                .realm(realm) //

                .grantType(OAuth2Constants.PASSWORD) //

                .clientId(clientId) //

                .clientSecret(clientSecret) //

                .username("idm-admin") //

                .password("admin") //

                .build();


        UserRepresentation user = new UserRepresentation();

        user.setEnabled(true);

        user.setUsername("tester1");

        user.setFirstName("First");

        user.setLastName("Last");

        user.setEmail("tom+tester1@tdlabs.local");

        user.setAttributes(Collections.singletonMap("origin", Arrays.asList("demo")));


        // Get realm

        RealmResource realmResource = keycloak.realm(realm);

        UsersResource userRessource = realmResource.users();


        // Create user (requires manage-users role)

        Response response = userRessource.create(user);

        System.out.println("Repsonse: " + response.getStatusInfo());

        System.out.println(response.getLocation());

        String userId = response.getLocation().getPath().replaceAll(".*/([^/]+)$", "$1");


        System.out.printf("User created with userId: %s%n", userId);


        // Get realm role "tester" (requires view-realm role)

        RoleRepresentation testerRealmRole = realmResource.roles()//

                .get("tester").toRepresentation();


        // Assign realm role tester to user

        userRessource.get(userId).roles().realmLevel() //

                .add(Arrays.asList(testerRealmRole));



但在我的行 Response response = userRessource.create(user); IDE 显示错误:无法访问 javax.ws.rs.core.Response;我正在使用 spring-boot 2.0.5.RELEASE 和 java 1.8


慕妹3146593
浏览 165回答 3
3回答

慕沐林林

如果问题仅与无法访问 javax.ws.rs.core.Response如果您使用 Maven,则必须在 maven pom.xml 中添加以下依赖项。<dependency>&nbsp; &nbsp; <groupId>javax.ws.rs</groupId>&nbsp; &nbsp; <artifactId>javax.ws.rs-api</artifactId>&nbsp; &nbsp; <version>2.1.1</version></dependency>如果你没有使用 maven,请下载 jar 文件并在类路径中设置。

海绵宝宝撒

keycloak-admin-client可以在maven 存储库中找到依赖项试试这些依赖项,它对我有用。<dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.keycloak</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>keycloak-admin-client</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>6.0.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.fasterxml.jackson.core</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jackson-databind</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.9.8</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.apache.httpcomponents</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>httpclient</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>4.5.4</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.resteasy</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>resteasy-client</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.6.3.Final</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.resteasy</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>resteasy-multipart-provider</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.6.3.Final</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.resteasy</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>resteasy-jackson2-provider</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.6.3.Final</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.spec.javax.ws.rs</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.0.2.Final</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp;

互换的青春

这些图书馆对我有用。请注意版本,因为我的项目使用的是旧版本的 spring。我使用了一些旧的库。<dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.projectlombok</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>lombok</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <optional>true</optional>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.keycloak</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>keycloak-spring-boot-starter</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${keycloak.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.keycloak</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>keycloak-admin-client</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>15.0.2</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.resteasy</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>resteasy-client</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>${resteasy.version}</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.jboss.resteasy</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>resteasy-jackson2-provider</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1.0.Final</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.xml.bind</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jaxb-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.3.0</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency></dependencies>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java