|
|
@@ -1,10 +1,7 @@
|
|
|
-package com.zhongshu.iot.server.core.service.mqtt;
|
|
|
+package com.zhongshu.iot.server.core.service.device;
|
|
|
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
-import com.zhongshu.iot.client.model.mqtt.GateWayUserInfoAddParam;
|
|
|
-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.model.mqtt.*;
|
|
|
import com.zhongshu.iot.client.type.type.MqttUserState;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.dataConfig.MqttConfig;
|
|
|
@@ -36,6 +33,8 @@ import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
+ * 账号管理
|
|
|
+ *
|
|
|
* @author TRX
|
|
|
* @date 2024/5/17
|
|
|
*/
|
|
|
@@ -69,11 +68,9 @@ public class GateWayUserInfoService {
|
|
|
*/
|
|
|
public void initData() {
|
|
|
log.info("GateWayUserInfoService initData");
|
|
|
-
|
|
|
initDefaultUser();
|
|
|
|
|
|
refreshAllGateWayUser();
|
|
|
-
|
|
|
refreshAllGateWaySecurity();
|
|
|
}
|
|
|
|
|
|
@@ -114,7 +111,7 @@ public class GateWayUserInfoService {
|
|
|
ResultContent<GateWayUserInfo> resultContent = addGateWayUser(userParam);
|
|
|
if (resultContent.isSuccess()) {
|
|
|
gateWayUserInfo = resultContent.getContent();
|
|
|
- if(ObjectUtils.isEmpty(gateWay2User)) {
|
|
|
+ if (ObjectUtils.isEmpty(gateWay2User)) {
|
|
|
gateWay2User = new GateWay2User();
|
|
|
}
|
|
|
|
|
|
@@ -135,6 +132,32 @@ public class GateWayUserInfoService {
|
|
|
return gateWayUserInfo;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 添加 连接账号
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent createUser(GateWayUserInfoParam param) {
|
|
|
+ Long number = param.getNumber();
|
|
|
+ if (number == null || number == 0) {
|
|
|
+ return ResultContent.buildFail("number不符合要求");
|
|
|
+ }
|
|
|
+
|
|
|
+ GateWayUserInfoAddParam userParam = new GateWayUserInfoAddParam();
|
|
|
+ String userName = CommonUtil.generateRandomString(8);
|
|
|
+ String roleName = userName;
|
|
|
+ userParam.setUserName(userName);
|
|
|
+ userParam.setPassWord(CommonUtil.generateRandomString(16));
|
|
|
+ userParam.setRoleName(roleName);
|
|
|
+ userParam.setNumber(number);
|
|
|
+ ResultContent<GateWayUserInfo> resultContent = addGateWayUser(userParam);
|
|
|
+ if (resultContent.isFailed()) {
|
|
|
+ return ResultContent.buildFail(resultContent.getMsg());
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 添加用户 (从网关注册开始)
|
|
|
*
|
|
|
@@ -153,6 +176,7 @@ public class GateWayUserInfoService {
|
|
|
BeanUtils.copyProperties(param, entity);
|
|
|
entity.setState(MqttUserState.Enable);
|
|
|
gateWayUserInfoDao.save(entity);
|
|
|
+
|
|
|
// 同步用户
|
|
|
syncUserToMQTTService(entity);
|
|
|
return ResultContent.buildSuccess(entity);
|
|
|
@@ -184,7 +208,7 @@ public class GateWayUserInfoService {
|
|
|
// 同步
|
|
|
if (param.getState() == MqttUserState.Disable) {
|
|
|
// 不过不可用,则删除QMTT服务上的用户信息
|
|
|
- deleteGateWayUser(entity);
|
|
|
+ removeUserToMQTTService(entity);
|
|
|
} else {
|
|
|
// 添加用户
|
|
|
syncUserToMQTTService(entity);
|
|
|
@@ -204,8 +228,9 @@ public class GateWayUserInfoService {
|
|
|
return ResultContent.buildFail(String.format("%s 账号不存在", param.getUserName()));
|
|
|
}
|
|
|
gateWayUserInfoDao.delete(entity);
|
|
|
+
|
|
|
// 删除服务文件上的用户
|
|
|
- deleteGateWayUser(entity);
|
|
|
+ removeUserToMQTTService(entity);
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
@@ -297,6 +322,15 @@ public class GateWayUserInfoService {
|
|
|
addressControl.removeUser(gateWayUserInfo.getUserName());
|
|
|
}
|
|
|
addressControl.addUser(gateWayUserInfo.getUserName(), gateWayUserInfo.getPassWord(), gateWayUserInfo.getRoleName(), false);
|
|
|
+ // 添加用户的默认权限
|
|
|
+ List<String> security = JMXUtil.buildGateWayUserDefault(gateWayUserInfo);
|
|
|
+ if (ObjectUtils.isNotEmpty(security)) {
|
|
|
+ String roleName = gateWayUserInfo.getRoleName();
|
|
|
+ for (String addressMatch : security) {
|
|
|
+ addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
mqtt2User.setGateWayUserInfo(gateWayUserInfo);
|
|
|
mqtt2User.setUserName(gateWayUserInfo.getUserName());
|
|
|
|
|
|
@@ -321,6 +355,47 @@ public class GateWayUserInfoService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public ResultContent removeUserToMQTTService(GateWayUserInfo gateWayUserInfo) {
|
|
|
+ //todo删除用户
|
|
|
+ List<MqttInfo> list = mqttInfoDao.findAll();
|
|
|
+ log.info("removeUserToMQTTService {}", list.size());
|
|
|
+ if (ObjectUtils.isNotEmpty(list)) {
|
|
|
+ for (MqttInfo mqttInfo : list) {
|
|
|
+ removeMqttUsers(mqttInfo, List.of(gateWayUserInfo));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void removeMqttUsers(MqttInfo mqttInfo, List<GateWayUserInfo> gateWayUserInfoList) {
|
|
|
+ if (ObjectUtils.isNotEmpty(mqttInfo) && ObjectUtils.isNotEmpty(gateWayUserInfoList)) {
|
|
|
+ try {
|
|
|
+ String urlStr = JMXUtil.buildServiceURL(mqttInfo);
|
|
|
+ JMXServiceURL url = new JMXServiceURL(urlStr);
|
|
|
+ @Cleanup JMXConnector connector = JMXConnectorFactory.connect(url, null);
|
|
|
+ connector.connect();
|
|
|
+ log.info("JMX removeMqttUsers {}:{} 连接成功...", 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 (GateWayUserInfo gateWayUserInfo : gateWayUserInfoList) {
|
|
|
+ try {
|
|
|
+ String oldUserStr = addressControl.listUser(gateWayUserInfo.getUserName());
|
|
|
+ if (JMXUtil.mqttUserIsExit(oldUserStr)) {
|
|
|
+ addressControl.removeUser(gateWayUserInfo.getUserName());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("removeMqttUsers: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 删除mqtt用户
|
|
|
*
|
|
|
@@ -384,6 +459,13 @@ public class GateWayUserInfoService {
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
+ public ResultContent syncSecurityToMQTTService(GateWayUserInfo gateWayUserInfo) {
|
|
|
+ if (ObjectUtils.isNotEmpty(gateWayUserInfo)) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
public ResultContent syncSecurityToMQTTService(List<GateWayInfo> gateWayInfos) {
|
|
|
//todo同步权限
|
|
|
List<MqttInfo> list = mqttInfoDao.findAll();
|
|
|
@@ -459,8 +541,7 @@ public class GateWayUserInfoService {
|
|
|
gateWayMqttSecurity.setMqttName(mqttInfo.getName());
|
|
|
gateWayMqttSecurity.setAddressMatch(addressMatch);
|
|
|
try {
|
|
|
- addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName,
|
|
|
- roleName, roleName, roleName, roleName, roleName, roleName, roleName);
|
|
|
+ addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
|
|
|
|
|
|
gateWayMqttSecurity.setIsSync(Boolean.TRUE);
|
|
|
gateWayMqttSecurity.setMsg("同步成功");
|