2018年8月17日 | Leave a comment 问题: keycloak spring 如何获取 所有用户? 解决: 原作者提供的 demo https://github.com/xtremebiker/keycloak-user-retrieval 核心代码是用 KeycloakRestTemplate KeycloakRestTemplate keycloakRest; @Value("${keycloak.auth-server-url}") String keycloakAuthServerUrl; @Autowired public void setKeycloakClientRequestFactory(KeycloakClientRequestFactory keycloakClientRequestFactory){ keycloakRest = new KeycloakRestTemplate(keycloakClientRequestFactory); } .... final List list = keycloakRest.getForEntity(URI.create(keycloakAuthServerUrl + "/admin/realms/tcheck/users"), List.class) .getBody(); 1234567891011 KeycloakRestTemplate keycloakRest; @Value("${keycloak.auth-server-url}") String keycloakAuthServerUrl; @Autowired public void setKeycloakClientRequestFactory(KeycloakClientRequestFactory keycloakClientRequestFactory){ keycloakRest = new KeycloakRestTemplate(keycloakClientRequestFactory); } .... final List list = keycloakRest.getForEntity(URI.create(keycloakAuthServerUrl + "/admin/realms/tcheck/users"), List.class) .getBody(); ** 另外一种思路是用 keycloak admin rest api ,但没搞懂该如何提供调用者身份信息 参考: https://stackoverflow.com/questions/48583361/how-can-i-read-all-users-using-keycloak-and-spring