|
|
@@ -6,10 +6,7 @@ import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoModel;
|
|
|
import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoNameParam;
|
|
|
import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoSearchParam;
|
|
|
import com.zhongshu.iot.client.type.type.MqttUserState;
|
|
|
-import com.zhongshu.iot.server.core.dao.mqtt.GateWay2UserDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.mqtt.GateWayUserInfoDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.mqtt.Mqtt2UserDao;
|
|
|
-import com.zhongshu.iot.server.core.dao.mqtt.MqttInfoDao;
|
|
|
+import com.zhongshu.iot.server.core.dao.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.util.CommonUtil;
|
|
|
import com.zhongshu.iot.server.core.util.DateUtils;
|
|
|
@@ -33,7 +30,9 @@ import javax.management.ObjectName;
|
|
|
import javax.management.remote.JMXConnector;
|
|
|
import javax.management.remote.JMXConnectorFactory;
|
|
|
import javax.management.remote.JMXServiceURL;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author TRX
|
|
|
@@ -55,6 +54,11 @@ public class GateWayUserInfoService {
|
|
|
@Autowired
|
|
|
private GateWay2UserDao gateWay2UserDao;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private GateWayMqttSecurityDao gateWayMqttSecurityDao;
|
|
|
+ @Autowired
|
|
|
+ private GateWay2DeviceDao gateWay2DeviceDao;
|
|
|
+
|
|
|
/**
|
|
|
* 初始mqtt连接账号信息
|
|
|
*
|
|
|
@@ -322,6 +326,97 @@ public class GateWayUserInfoService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public ResultContent syncSecurityToMQTTService(GateWayInfo gateWayInfo) {
|
|
|
+ //todo同步权限
|
|
|
+ List<MqttInfo> list = mqttInfoDao.findAll();
|
|
|
+ log.info("syncSecurityToMQTTService {}", list.size());
|
|
|
+ if (ObjectUtils.isNotEmpty(list)) {
|
|
|
+ for (MqttInfo mqttInfo : list) {
|
|
|
+ syncMqttSecuritySettings(mqttInfo, List.of(gateWayInfo));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步网关可监听的权限
|
|
|
+ *
|
|
|
+ * @param mqttInfo
|
|
|
+ * @param gateWayInfos
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public void syncMqttSecuritySettings(MqttInfo mqttInfo, List<GateWayInfo> gateWayInfos) {
|
|
|
+ if (ObjectUtils.isNotEmpty(mqttInfo) && ObjectUtils.isNotEmpty(gateWayInfos)) {
|
|
|
+ try {
|
|
|
+ String urlStr = JMXUtil.buildServiceURL(mqttInfo);
|
|
|
+ JMXServiceURL url = new JMXServiceURL(urlStr);
|
|
|
+ @Cleanup JMXConnector connector = JMXConnectorFactory.connect(url, null);
|
|
|
+ connector.connect();
|
|
|
+ log.info("JMX {}:{} 连接成功...", mqttInfo.getJmxHost(), mqttInfo.getJmxPort());
|
|
|
+ MBeanServerConnection connection = connector.getMBeanServerConnection();
|
|
|
+ ObjectName addressObjectName = ObjectNameBuilder.create("org.apache.activemq.artemis", mqttInfo.getBrokerName()).getActiveMQServerObjectName();
|
|
|
+ ActiveMQServerControl addressControl = MBeanServerInvocationHandler.newProxyInstance(connection, addressObjectName, ActiveMQServerControl.class, false);
|
|
|
+
|
|
|
+ for (GateWayInfo gateWayInfo : gateWayInfos) {
|
|
|
+ // 以前的权限
|
|
|
+ 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<GateWayMqttSecurity> securities = new ArrayList<>();
|
|
|
+ addressMatchs.parallelStream().forEach(addressMatch -> {
|
|
|
+ GateWayMqttSecurity gateWayMqttSecurity = new GateWayMqttSecurity();
|
|
|
+ gateWayMqttSecurity.setUserName(userName);
|
|
|
+ gateWayMqttSecurity.setRoleName(roleName);
|
|
|
+ gateWayMqttSecurity.setGateWayInfo(gateWayInfo);
|
|
|
+ gateWayMqttSecurity.setGateWayId(gateWayInfo.getGateWayId());
|
|
|
+ gateWayMqttSecurity.setMqttInfo(mqttInfo);
|
|
|
+ gateWayMqttSecurity.setMqttName(mqttInfo.getName());
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
//-----------------------------同步权限数据 end----------------------
|
|
|
|
|
|
/**
|