Ver Fonte

refactor: 小程序获取及更新用户属性代码重构

zengweijie há 3 anos atrás
pai
commit
37aa11adc8

+ 9 - 0
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/keycloak/KeycloakServiceImpl.java

@@ -173,6 +173,15 @@ public class KeycloakServiceImpl implements KeycloakService {
         }
     }
 
+    @Override
+    public List<KeycloakUserCO> searchUserByAttrEntry(List<Map.Entry<String, String>> entries) {
+        String search = entries.stream()
+            .filter(t -> Objects.nonNull(t) && Objects.nonNull(t.getKey()) && Objects.nonNull(t.getValue()))
+            .map(t -> t.getKey() + ":" + t.getValue())
+            .collect(Collectors.joining(" "));
+        return keycloak.realm(realm).users().searchByAttributes(search).stream().map(this::transfer).collect(Collectors.toList());
+    }
+
     private List<KeycloakUserCO> getUserByRoleAndGroup(String group, String role) {
         if (!StringUtils.hasText(group) || !StringUtils.hasText(role)) {
             return Collections.emptyList();

+ 8 - 0
smsb-customer-manager-client/src/main/java/com/inspur/customer/service/client/keycloak/KeycloakService.java

@@ -61,4 +61,12 @@ public interface KeycloakService {
     List<KeycloakUserCO> getGroupAdmin(String group);
 
     String getSingleAttrByGroupPath(String group, String key);
+
+    /**
+     * 根据属性键值对查找用户
+     *
+     * @param entries 属性键值对
+     * @return 找到的用户
+     */
+    List<KeycloakUserCO> searchUserByAttrEntry(List<Map.Entry<String, String>> entries);
 }

+ 37 - 0
smsb-customer-manager-start-web/src/test/java/com/inspur/customer/KeycloakTest.java

@@ -0,0 +1,37 @@
+package com.inspur.customer;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.keycloak.admin.client.Keycloak;
+import org.keycloak.representations.idm.UserRepresentation;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * @author zengweijie
+ * @version 1.0
+ * @date 2022/6/22 17:32
+ **/
+@Slf4j
+@SpringBootTest
+public class KeycloakTest {
+    @Resource
+    Keycloak keycloak;
+    @Value("${keycloak.realm}")
+    private String realm;
+
+    @Test
+    void test() {
+        List<UserRepresentation> userRepresentations = keycloak.realm(realm).users().searchByAttributes("wechat-applet-openid:oLBkj42qubLayXZWmFlGnKh5X");
+        for (UserRepresentation userRepresentation : userRepresentations) {
+            log.info(userRepresentation.toString());
+            log.info(userRepresentation.getUsername());
+            Optional.ofNullable(userRepresentation.getAttributes()).ifPresent(t -> log.info(t.toString()));
+            log.info("_______________________________");
+        }
+    }
+}