瀏覽代碼

更新!

TRX 1 年之前
父節點
當前提交
2ea4623210

+ 23 - 58
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/JMXSyncService.java

@@ -109,6 +109,7 @@ public class JMXSyncService {
                 ObjectName addressObjectName = ObjectNameBuilder.create("org.apache.activemq.artemis", mqttInfo.getBrokerName()).getActiveMQServerObjectName();
                 ActiveMQServerControl addressControl = MBeanServerInvocationHandler.newProxyInstance(connection, addressObjectName, ActiveMQServerControl.class, false);
 
+                String adminRole = MqttConfig.adminRoleName;
                 for (GateWayUserInfo gateWayUserInfo : gateWayUserInfoList) {
                     Mqtt2User mqtt2User = mqtt2UserDao.findTopByMqttInfoAndGateWayUserInfo(mqttInfo, gateWayUserInfo);
                     if (ObjectUtils.isEmpty(mqtt2User)) {
@@ -119,20 +120,26 @@ public class JMXSyncService {
                         if (JMXUtil.mqttUserIsExit(oldUserStr)) {
                             addressControl.removeUser(gateWayUserInfo.getUserName());
                         }
+
                         // 添加用户的默认权限
                         List<String> security = new ArrayList<>();
-//                        List<String> security = JMXUtil.buildGateWayUserDefault(gateWayUserInfo);
                         // 添加用户
                         addressControl.addUser(gateWayUserInfo.getUserName(), gateWayUserInfo.getPassWord(), gateWayUserInfo.getRoleName(), false);
+
                         String userName = gateWayUserInfo.getUserName();
                         this.applicationContext.getBeansOfType(PlatformTopic.class).values().forEach((it) -> {
                             String topic = String.format(it.topic(), userName);
                             security.add(topic);
                             security.add(String.format("%s/#", topic));
                         });
+
                         if (ObjectUtils.isNotEmpty(security)) {
                             List<String> securites = security.stream().distinct().collect(Collectors.toList());
                             String roleName = gateWayUserInfo.getRoleName();
+                            List<String> roles = new ArrayList<>();
+                            roles.add(roleName);
+                            roles.add(adminRole);
+                            roleName = String.join(",", roles);
                             for (String addressMatch : securites) {
                                 addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
                             }
@@ -446,65 +453,23 @@ public class JMXSyncService {
                     if (ObjectUtils.isEmpty(gateWayUserInfo)) {
                         continue;
                     }
+                    List<String> addressMatchs = JMXUtil.buildDeviceAddressMatch(deviceInfo);
+                    if (ObjectUtils.isNotEmpty(addressMatchs)) {
+                        String roleName = gateWayUserInfo.getRoleName();
 
+                        List<String> roleNames = new ArrayList<>();
+                        roleNames.add(roleName);
+                        roleNames.add(_roleName);
 
-//                    // 以前的权限
-//                    List<GateWayMqttSecurity> list = gateWayMqttSecurityDao.findByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
-//                    gateWayMqttSecurityDao.deleteByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
-//
-//                    // 查询网关关联的用户
-//                    GateWay2User gateWay2User = gateWay2UserDao.findTopByGateWayInfo(gateWayInfo);
-//                    if (ObjectUtils.isEmpty(gateWay2User)) {
-//                        log.error("网关对应用户信息为不存在");
-//                        continue;
-//                    }
-//                    GateWayUserInfo gateWayUserInfo = gateWay2User.getGateWayUserInfo();
-//                    if (ObjectUtils.isEmpty(gateWayUserInfo)) {
-//                        log.error("网关对应用户信息为空");
-//                        continue;
-//                    }
-//                    // 查询网关对应的设备
-//                    List<GateWay2Device> gateWay2Devices = gateWay2DeviceDao.findByGateWayInfo(gateWayInfo);
-//                    List<DeviceInfo> deviceInfos = new ArrayList<>();
-//                    if (ObjectUtils.isNotEmpty(gateWay2Devices)) {
-//                        deviceInfos = gateWay2Devices.stream().map(it -> it.getDeviceInfo()).collect(Collectors.toList());
-//                    }
-//
-//                    List<String> addressMatchs = JMXUtil.buildAddressMatch(gateWayInfo, deviceInfos);
-//                    if (ObjectUtils.isNotEmpty(addressMatchs)) {
-//                        String userName = gateWayUserInfo.getUserName();
-//                        String roleName = gateWayUserInfo.getRoleName();
-//
-//                        List<String> roleNames = new ArrayList<>();
-//                        roleNames.add(roleName);
-//                        roleNames.add(_roleName);
-//
-//                        roleName = String.join(",", roleNames);
-//                        List<GateWayMqttSecurity> securities = new ArrayList<>();
-//                        for (String addressMatch : addressMatchs) {
-//                            GateWayMqttSecurity gateWayMqttSecurity = new GateWayMqttSecurity();
-//                            gateWayMqttSecurity.setUserName(userName);
-//                            gateWayMqttSecurity.setRoleName(roleName);
-//                            gateWayMqttSecurity.setGateWayInfo(gateWayInfo);
-//                            gateWayMqttSecurity.setGateWayId(gateWayInfo.getGateWayId());
-//                            gateWayMqttSecurity.setMqttInfo(mqttInfo);
-//                            gateWayMqttSecurity.setMqttName(mqttInfo.getName());
-//                            gateWayMqttSecurity.setAddressMatch(addressMatch);
-//                            try {
-//                                addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
-//
-//                                gateWayMqttSecurity.setIsSync(Boolean.TRUE);
-//                                gateWayMqttSecurity.setMsg("同步成功");
-//                            } catch (Exception e) {
-//                                e.printStackTrace();
-//                                gateWayMqttSecurity.setIsSync(Boolean.FALSE);
-//                                gateWayMqttSecurity.setMsg(String.format("同步失败:%s", e.getMessage()));
-//                            }
-//                            gateWayMqttSecurity.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-//                            securities.add(gateWayMqttSecurity);
-//                        }
-//                        gateWayMqttSecurityDao.saveAll(securities);
-//                    }
+                        roleName = String.join(",", roleNames);
+                        for (String addressMatch : addressMatchs) {
+                            try {
+                                addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
+                            } catch (Exception e) {
+                                e.printStackTrace();
+                            }
+                        }
+                    }
                 }
             } catch (Exception e) {
                 e.printStackTrace();

+ 8 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/util/JMXUtil.java

@@ -71,6 +71,14 @@ public class JMXUtil {
         return list;
     }
 
+    public static List<String> buildDeviceAddressMatch(DeviceInfo deviceInfo) {
+        List<String> list = new ArrayList<String>();
+        if (ObjectUtils.isNotEmpty(deviceInfo)) {
+            list.add(String.format("/device/%s/#", deviceInfo.getDeviceId()));
+        }
+        return list;
+    }
+
     public static List<String> buildGateWayAddressMatch(GateWayInfo gateWayInfo) {
         List<String> list = new ArrayList<String>();
         if (ObjectUtils.isNotEmpty(gateWayInfo)) {