Просмотр исходного кода

feat: 返回的管理员token中增加租户字段

zengweijie 3 лет назад
Родитель
Сommit
5730104b27

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

@@ -183,6 +183,19 @@ public class KeycloakServiceImpl implements KeycloakService {
         return keycloak.realm(realm).users().searchByAttributes(search).stream().map(this::transfer).collect(Collectors.toList());
     }
 
+    @Override
+    public String getUserTenant(String userId) {
+        List<GroupRepresentation> groups = keycloak.realm(realm).users().get(userId).groups();
+        if (groups.isEmpty()) {
+            return null;
+        }
+        String path = groups.get(0).getPath();
+        if (path.lastIndexOf('/') == 0) {
+            return path;
+        }
+        return path.substring(0, path.indexOf('/', 1));
+    }
+
     private List<KeycloakUserCO> getUserByRoleAndGroup(String group, String role) {
         if (!StringUtils.hasText(group) || !StringUtils.hasText(role)) {
             return Collections.emptyList();

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

@@ -71,4 +71,6 @@ public interface KeycloakService {
      * @return 找到的用户
      */
     List<KeycloakUserCO> searchUserByAttrEntry(List<Pair> entries);
+
+    String getUserTenant(String userId);
 }

+ 5 - 7
smsb-customer-manager-start-web/src/test/java/com/inspur/customer/KeycloakTest.java

@@ -1,5 +1,6 @@
 package com.inspur.customer;
 
+import com.inspur.customer.service.client.keycloak.KeycloakService;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.jupiter.api.Test;
 import org.keycloak.admin.client.Keycloak;
@@ -24,14 +25,11 @@ public class KeycloakTest {
     @Value("${keycloak.realm}")
     private String realm;
 
+    @Resource
+    KeycloakService keycloakService;
+
     @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("_______________________________");
-        }
+        log.info(keycloakService.getUserTenant("9d0bcc7c-7fc3-492a-954d-c3f86246bbcd"));
     }
 }