我在我的应用程序中使用keycloak管理客户端
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-admin-client</artifactId>
<version>${keycloak.version}</version>
</dependency>
在我代码中,我有:
package br.com.fabioebner.api.serivce;
import lombok.extern.slf4j.Slf4j;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
@Slf4j
public class KeycloakService {
private final RealmRepresentation realm;
private final Keycloak keycloak;
public KeycloakService() {
this.keycloak = Keycloak.getInstance(
"https://myIp/auth/",
"master",
"admin",
"password",
"admin-cli");
this.realm = keycloak.realm("myRealm").toRepresentation();
}
@EventListener(ApplicationReadyEvent.class)
public void addUser(){
List<UserRepresentation> users = realm.getUsers();
users.forEach(user -> {
log.debug(user.getUsername());
});
}
}
为什么realm.getUsers()
总是返回null??
如果我把realm.getClients()
也返回null,但是我的领域“myRealm”中有用户和客户机。
1条答案
按热度按时间sczxawaw1#
问题是您从
Realm
* 表示 * 中获取用户试图从
Realm
中得到它们来代替,即: