|  |  | package com.sunyo.wlpt.message.bus.service.service.impl; | 
|  |  |  | 
|  |  | import com.rabbitmq.http.client.Client; | 
|  |  | import com.rabbitmq.http.client.domain.TopicPermissions; | 
|  |  | import com.rabbitmq.http.client.domain.UserPermissions; | 
|  |  | import com.github.pagehelper.PageHelper; | 
|  |  | import com.github.pagehelper.PageInfo; | 
|  |  | import com.sunyo.wlpt.message.bus.service.domain.BusQueue; | 
|  |  | import com.sunyo.wlpt.message.bus.service.domain.BusServer; | 
|  |  | import com.sunyo.wlpt.message.bus.service.domain.UserInfo; | 
|  |  | import com.sunyo.wlpt.message.bus.service.domain.VirtualHost; | 
|  |  | import com.sunyo.wlpt.message.bus.service.mapper.UserInfoMapper; | 
|  |  | import com.sunyo.wlpt.message.bus.service.rabbit.utils.ClientUtils; | 
|  |  | import com.sunyo.wlpt.message.bus.service.rabbit.utils.RabbitUtils; | 
|  |  | import com.sunyo.wlpt.message.bus.service.response.ResultJson; | 
|  |  | import com.sunyo.wlpt.message.bus.service.service.BusQueueService; | 
| ... | ... | @@ -16,16 +16,16 @@ import com.sunyo.wlpt.message.bus.service.service.UserInfoService; | 
|  |  | import com.sunyo.wlpt.message.bus.service.service.VirtualHostService; | 
|  |  | import com.sunyo.wlpt.message.bus.service.utils.IdUtils; | 
|  |  | import io.netty.util.internal.StringUtil; | 
|  |  | import lombok.extern.slf4j.Slf4j; | 
|  |  | import org.springframework.beans.factory.annotation.Value; | 
|  |  | import org.springframework.boot.autoconfigure.amqp.RabbitProperties; | 
|  |  | import org.springframework.stereotype.Service; | 
|  |  | import org.springframework.util.DigestUtils; | 
|  |  | import org.springframework.transaction.annotation.Propagation; | 
|  |  | import org.springframework.transaction.annotation.Transactional; | 
|  |  |  | 
|  |  | import javax.annotation.Resource; | 
|  |  | import java.io.IOException; | 
|  |  | import java.net.MalformedURLException; | 
|  |  | import java.net.URISyntaxException; | 
|  |  | import java.util.ArrayList; | 
|  |  | import java.util.List; | 
|  |  | import java.util.concurrent.TimeoutException; | 
|  |  |  | 
| ... | ... | @@ -34,6 +34,7 @@ import java.util.concurrent.TimeoutException; | 
|  |  | * Description: | 
|  |  | * 时间:2020/6/29 17:15 | 
|  |  | */ | 
|  |  | @Slf4j | 
|  |  | @Service | 
|  |  | public class UserInfoServiceImpl implements UserInfoService { | 
|  |  |  | 
| ... | ... | @@ -82,33 +83,82 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | return userInfoMapper.insert(record); | 
|  |  | } | 
|  |  |  | 
|  |  | /** | 
|  |  | * 在MQ服务器上,添加用户信息,并根据一个or多个虚拟主机id(由于其中包含着服务器id,故不做个数判断),设置用户与虚拟主机关系 | 
|  |  | * <p> | 
|  |  | * 并创建用户对应的队列 | 
|  |  | * <p> | 
|  |  | * 并存储于数据库 | 
|  |  | * | 
|  |  | * @param userInfo {@link UserInfo} | 
|  |  | * @return {@link ResultJson} | 
|  |  | * @throws IOException | 
|  |  | * @throws URISyntaxException | 
|  |  | * @throws TimeoutException | 
|  |  | */ | 
|  |  | @Override | 
|  |  | @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED) | 
|  |  | public ResultJson insertSelective(UserInfo userInfo) throws IOException, URISyntaxException, TimeoutException | 
|  |  | { | 
|  |  | String password = userInfo.getPassword(); | 
|  |  | String virtualHostId = userInfo.getVirtualHostId(); | 
|  |  | String splitItem = ","; | 
|  |  | if (virtualHostId.contains(splitItem)) { | 
|  |  | int index = 0; | 
|  |  | String[] split_hostId = virtualHostId.split(splitItem); | 
|  |  | for (int i = 0; i < split_hostId.length; i++) { | 
|  |  | VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(split_hostId[i]); | 
|  |  | if (virtualHost != null) { | 
|  |  | userInfo.setVirtualHostId(virtualHost.getId()); | 
|  |  | userInfo.setVirtualHostName(virtualHost.getVirtualHostName()); | 
|  |  |  | 
|  |  | VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(userInfo.getVirtualHostId()); | 
|  |  | userInfo.setVirtualHostName(virtualHost.getVirtualHostName()); | 
|  |  | BusServer busServer = busServerService.selectByPrimaryKey(userInfo.getServerId()); | 
|  |  | BusServer busServer = busServerService.selectByPrimaryKey(virtualHost.getServerId()); | 
|  |  |  | 
|  |  | userInfo.setServerId(busServer.getId()); | 
|  |  | userInfo.setServerName(busServer.getServerName()); | 
|  |  |  | 
|  |  | ResultJson validateResult = validateUser(userInfo); | 
|  |  | if (!"200".equals(validateUser(userInfo).getCode())) { | 
|  |  | return validateResult; | 
|  |  | } | 
|  |  | userInfo.setId(IdUtils.generateId()); | 
|  |  | userInfo.setPassword(DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes())); | 
|  |  |  | 
|  |  | int num = userInfoMapper.insertSelective(userInfo); | 
|  |  | // 1.根据用户信息,在MQ创建用户 | 
|  |  | addMQUser(userInfo, password); | 
|  |  | // 3.在对应虚拟机下创建队列 | 
|  |  | ClientUtils.addRabbitUser(userInfo, busServer, password); | 
|  |  | ClientUtils.userRelation(userInfo, busServer); | 
|  |  | createQueue(userInfo); | 
|  |  | index = index + num; | 
|  |  | } else { | 
|  |  | log.warn(split_hostId[i] + "->是服务器id"); | 
|  |  | } | 
|  |  | } | 
|  |  | return index > 0 | 
|  |  | ? new ResultJson<>("200", "添加用户信息,成功") | 
|  |  | : new ResultJson<>("500", "添加用户信息,失败"); | 
|  |  | } else { | 
|  |  | VirtualHost virtualHost = virtualHostService.selectByPrimaryKey(userInfo.getVirtualHostId()); | 
|  |  | int num = 0; | 
|  |  | if (virtualHost != null) { | 
|  |  | userInfo.setVirtualHostId(virtualHost.getId()); | 
|  |  | userInfo.setVirtualHostName(virtualHost.getVirtualHostName()); | 
|  |  |  | 
|  |  | userInfo.setVirtualHostName(virtualHost.getVirtualHostName()); | 
|  |  | BusServer busServer = busServerService.selectByPrimaryKey(virtualHost.getServerId()); | 
|  |  |  | 
|  |  | userInfo.setServerId(busServer.getId()); | 
|  |  | userInfo.setServerName(busServer.getServerName()); | 
|  |  |  | 
|  |  | userInfo.setId(IdUtils.generateId()); | 
|  |  |  | 
|  |  | num = userInfoMapper.insertSelective(userInfo); | 
|  |  | ClientUtils.addRabbitUser(userInfo, busServer, password); | 
|  |  | ClientUtils.userRelation(userInfo, busServer); | 
|  |  | createQueue(userInfo); | 
|  |  | } else { | 
|  |  | log.error(userInfo.getVirtualHostId() + "->并不是虚拟主机id;故添加失败"); | 
|  |  | } | 
|  |  | return num > 0 | 
|  |  | ? new ResultJson<>("200", "添加用户信息,成功") | 
|  |  | : new ResultJson<>("500", "添加用户信息,失败"); | 
|  |  | } | 
|  |  | } | 
|  |  |  | 
|  |  | /** | 
|  |  | * 根据用户信息,添加队列 | 
| ... | ... | @@ -121,9 +171,8 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | { | 
|  |  | BusQueue busQueue = BusQueue.builder() | 
|  |  | .id(IdUtils.generateId()) | 
|  |  | .userId(userInfo.getId()) | 
|  |  | .username(userInfo.getUsername()) | 
|  |  | .queueName(userInfo.getUsername() + "_R") | 
|  |  | .queueName(userInfo.getVirtualHostName() + "_" + userInfo.getUsername() + "_R") | 
|  |  | .virtualHostId(userInfo.getVirtualHostId()) | 
|  |  | .durability(true) | 
|  |  | .autoDelete(false) | 
| ... | ... | @@ -132,36 +181,6 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | busQueueService.insertSelective(busQueue); | 
|  |  | } | 
|  |  |  | 
|  |  |  | 
|  |  | /** | 
|  |  | * 添加MQ用户信息,并配置MQ用户与虚拟主机的关系 | 
|  |  | * | 
|  |  | * @param userInfo {@link UserInfo} | 
|  |  | */ | 
|  |  | public void addMQUser(UserInfo userInfo, String password) throws MalformedURLException, URISyntaxException | 
|  |  | { | 
|  |  | String username = userInfo.getUsername(); | 
|  |  | String virtualHostName = userInfo.getVirtualHostName(); | 
|  |  |  | 
|  |  | String url = "http://" + host + ":15672/api"; | 
|  |  | Client client = new Client(url, rabbitUsername, rabbitPassword); | 
|  |  | ArrayList<String> list = new ArrayList<>(); | 
|  |  | client.createUser(username, password.toCharArray(), list); | 
|  |  |  | 
|  |  | UserPermissions p = new UserPermissions(); | 
|  |  | p.setConfigure(username + ".*"); | 
|  |  | p.setRead(username + ".*"); | 
|  |  | p.setWrite(username + ".*"); | 
|  |  | client.updatePermissions(virtualHostName, username, p); | 
|  |  |  | 
|  |  | TopicPermissions topicPermissions = new TopicPermissions(); | 
|  |  | topicPermissions.setVhost(virtualHostName); | 
|  |  | topicPermissions.setExchange(""); | 
|  |  | topicPermissions.setRead(".*"); | 
|  |  | topicPermissions.setWrite(".*"); | 
|  |  | client.updateTopicPermissions(virtualHostName, username, topicPermissions); | 
|  |  | } | 
|  |  |  | 
|  |  | @Override | 
|  |  | public UserInfo selectByPrimaryKey(String id) | 
|  |  | { | 
| ... | ... | @@ -169,12 +188,6 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | } | 
|  |  |  | 
|  |  | @Override | 
|  |  | public UserInfo selectByUsername(String username) | 
|  |  | { | 
|  |  | return userInfoMapper.selectByUsername(username); | 
|  |  | } | 
|  |  |  | 
|  |  | @Override | 
|  |  | public ResultJson updateByPrimaryKeySelective(UserInfo userInfo) | 
|  |  | { | 
|  |  | if (StringUtil.isNullOrEmpty(userInfo.getId())) { | 
| ... | ... | @@ -189,7 +202,7 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | } | 
|  |  |  | 
|  |  | /** | 
|  |  | * 校验用户名称是否已经存在 | 
|  |  | * 校验用户信息 | 
|  |  | */ | 
|  |  | public ResultJson validateUser(UserInfo userInfo) | 
|  |  | { | 
| ... | ... | @@ -199,6 +212,7 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | if (StringUtil.isNullOrEmpty(userInfo.getServerName()) || StringUtil.isNullOrEmpty(userInfo.getVirtualHostName())) { | 
|  |  | return new ResultJson<>("400", "服务器和虚拟主机,不能为空"); | 
|  |  | } | 
|  |  |  | 
|  |  | String userId = userInfo.getId(); | 
|  |  | if (!StringUtil.isNullOrEmpty(userId)) { | 
|  |  | UserInfo oldUserInfo = userInfoMapper.selectByPrimaryKey(userId); | 
| ... | ... | @@ -207,9 +221,9 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | } | 
|  |  | return ResultJson.success("通过校验"); | 
|  |  | } else { | 
|  |  | List<UserInfo> userInfos = userInfoMapper.selectUserExist(userInfo.getUsername()); | 
|  |  | List<UserInfo> userInfos = userInfoMapper.validateUserInfo(userInfo); | 
|  |  | return userInfos.size() > 0 | 
|  |  | ? new ResultJson<>("400", "该用户,已存在") | 
|  |  | ? new ResultJson<>("400", "该用户信息,已存在") | 
|  |  | : ResultJson.success("通过校验"); | 
|  |  | } | 
|  |  | } | 
| ... | ... | @@ -233,7 +247,7 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | } | 
|  |  |  | 
|  |  | @Override | 
|  |  | public ResultJson updatePassword(UserInfo userInfo) throws MalformedURLException, URISyntaxException | 
|  |  | public ResultJson updatePassword(UserInfo userInfo) throws IOException, URISyntaxException | 
|  |  | { | 
|  |  | if (StringUtil.isNullOrEmpty(userInfo.getUsername()) || StringUtil.isNullOrEmpty(userInfo.getPassword())) { | 
|  |  | return new ResultJson<>("400", "用户名和密码,不能为空"); | 
| ... | ... | @@ -241,25 +255,70 @@ public class UserInfoServiceImpl implements UserInfoService { | 
|  |  | if (userInfoMapper.selectUserExist(userInfo.getUsername()).size() == 0) { | 
|  |  | return new ResultJson<>("400", "该用户不存在"); | 
|  |  | } | 
|  |  | UserInfo oldUserInfo = userInfoMapper.selectByUsername(userInfo.getUsername()); | 
|  |  | oldUserInfo.setPassword(DigestUtils.md5DigestAsHex(userInfo.getPassword().getBytes())); | 
|  |  | int num = userInfoMapper.updateByPrimaryKeySelective(oldUserInfo); | 
|  |  | // 在这里修改MQ用户密码 | 
|  |  | Client client = connectClient(); | 
|  |  | ArrayList<String> tags = new ArrayList<>(); | 
|  |  | client.updateUser(userInfo.getUsername(), userInfo.getPassword().toCharArray(), tags); | 
|  |  | return num > 0 | 
|  |  | ? new ResultJson<>("200", userInfo.getUsername() + "用户:修改密码成功!") | 
|  |  | : new ResultJson<>("500", userInfo.getUsername() + "用户:修改密码失败!"); | 
|  |  | BusServer busServer = busServerService.selectByPrimaryKey(userInfo.getServerId()); | 
|  |  | String newPassword = userInfo.getPassword(); | 
|  |  | ClientUtils.updatePassword(busServer, userInfo.getUsername(), newPassword); | 
|  |  | return new ResultJson<>("200", "修改密码成功"); | 
|  |  | } | 
|  |  |  | 
|  |  | @Override | 
|  |  | public ResultJson selectUserInfoList(UserInfo userInfo, Integer pageNum, Integer pageSize) | 
|  |  | { | 
|  |  | PageHelper.startPage(pageNum, pageSize); | 
|  |  | List<UserInfo> userInfoList = userInfoMapper.selectUserInfoList(userInfo); | 
|  |  | PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList); | 
|  |  | return pageInfo.getTotal() > 0 | 
|  |  | ? new ResultJson<>("200", "查询用户信息列表,成功!", pageInfo) | 
|  |  | : new ResultJson<>("500", "查询用户信息列表,失败!"); | 
|  |  | } | 
|  |  |  | 
|  |  | public Client connectClient() throws MalformedURLException, URISyntaxException | 
|  |  | /** | 
|  |  | * 删除根据用户名删除该用户的所有关系 | 
|  |  | * <p> | 
|  |  | * QM服务器上删除该用户(会自动删除该用户关系) | 
|  |  | * <p> | 
|  |  | * MQ服务器(以及数据库)删除该用户对应队列(日后需要时,打开注释,即可) | 
|  |  | */ | 
|  |  | @Override | 
|  |  | public ResultJson deleteUserInfo(UserInfo userInfo) throws IOException, URISyntaxException | 
|  |  | { | 
|  |  | String url = "http://" + host + ":15672/api"; | 
|  |  | Client client = new Client(url, rabbitUsername, rabbitPassword); | 
|  |  | return client; | 
|  |  | String username = userInfo.getUsername(); | 
|  |  | List<UserInfo> userInfoList = userInfoMapper.selectByUsername(userInfo.getUsername()); | 
|  |  | int num = userInfoMapper.deleteByUsername(username); | 
|  |  | for (UserInfo item : userInfoList) { | 
|  |  | BusServer busServer = busServerService.selectByPrimaryKey(item.getServerId()); | 
|  |  | ClientUtils.deleteMQUser(username, busServer); | 
|  |  | } | 
|  |  |  | 
|  |  |  | 
|  |  | //        List<BusQueue> queueList = busQueueService.selectByUsername(username); | 
|  |  | //        for (BusQueue queue : queueList) { | 
|  |  | //            busQueueService.deleteByPrimaryKey(queue.getId()); | 
|  |  | //        } | 
|  |  |  | 
|  |  | return num > 0 | 
|  |  | ? new ResultJson<>("200", "删除用户信息,成功!") | 
|  |  | : new ResultJson<>("500", "删除用户信息,失败!"); | 
|  |  | } | 
|  |  |  | 
|  |  | /** | 
|  |  | * 删除用户关系(数据库 and MQ服务器) | 
|  |  | * | 
|  |  | * @param userInfo {@link UserInfo} | 
|  |  | * @return 返回结果 | 
|  |  | * @throws IOException        输入输出异常 | 
|  |  | * @throws URISyntaxException URI语法异常 | 
|  |  | */ | 
|  |  | @Override | 
|  |  | public ResultJson deleteUserRelation(UserInfo userInfo) throws IOException, URISyntaxException | 
|  |  | { | 
|  |  | int num = userInfoMapper.deleteByPrimaryKey(userInfo.getId()); | 
|  |  | BusServer busServer = busServerService.selectByPrimaryKey(userInfo.getServerId()); | 
|  |  | ClientUtils.clearPermissions(busServer, userInfo.getVirtualHostName(), userInfo.getUsername()); | 
|  |  | return num > 0 | 
|  |  | ? new ResultJson<>("200", "删除用户关系,成功!") | 
|  |  | : new ResultJson<>("500", "删除用户关系,失败!"); | 
|  |  | } | 
|  |  | } | 
|  |  |  | 
|  |  |  | 
... | ... |  |