gpt4 book ai didi

java - Keycloak 管理客户端

转载 作者:行者123 更新时间:2023-12-03 23:15:03 25 4
gpt4 key购买 nike

我尝试使用 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));

// Get client
ClientRepresentation app1Client = realmResource.clients() //
.findByClientId("app-javaee-petclinic").get(0);

// Get client level role (requires view-clients role)
RoleRepresentation userClientRole = realmResource.clients().get(app1Client.getId()) //
.roles().get("user").toRepresentation();

// Assign client level role to user
userRessource.get(userId).roles() //
.clientLevel(app1Client.getId()).add(Arrays.asList(userClientRole));

// Define password credential
CredentialRepresentation passwordCred = new CredentialRepresentation();
passwordCred.setTemporary(false);
passwordCred.setType(CredentialRepresentation.PASSWORD);
passwordCred.setValue("test");

// Set password credential
userRessource.get(userId).resetPassword(passwordCred);

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

最佳答案

如果问题仅与

Cannot access javax.ws.rs.core.Response



如果您使用 Maven,则必须在 maven pom.xml 中添加以下依赖项。
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.1.1</version>
</dependency>

如果你没有使用maven,下载jar文件并在classpath中设置。

关于java - Keycloak 管理客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56407763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com