Commit 9355271f authored by ma's avatar ma

完善安全报警模块

parent abd449a7
......@@ -24,6 +24,9 @@ public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
// 合并到yx需要添加上白名单
// .allowedOrigins(allowedOrigins)
// .allowedMethods(allowedMethods)
.allowedOrigins("*")
.allowedMethods("*")
.allowCredentials(true)
......
......@@ -21,10 +21,22 @@ import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ThreadPoolConfig {
private static final int corePoolSize = 1; // 核心线程数(默认线程数)
private static final int maxPoolSize = 2; // 最大线程数
private static final int keepAliveTime = 10; // 允许线程空闲时间(单位:默认为秒)
private static final int queueCapacity = 2; // 缓冲队列数
/**
* 核心线程数(默认线程数)
*/
private static final int CORE_POOL_SIZE = 1;
/**
* 最大线程数
*/
private static final int MAX_POOL_SIZE = 2;
/**
* 允许线程空闲时间(单位:默认为秒)
*/
private static final int KEEP_ALIVE_TIME = 10;
/**
* 缓冲队列数
*/
private static final int QUEUE_CAPACITY = 2;
/**
* 默认异步线程池
......@@ -35,10 +47,10 @@ public class ThreadPoolConfig {
public Executor taskExecutor() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setThreadNamePrefix("threadPoll-");
pool.setCorePoolSize(corePoolSize);
pool.setMaxPoolSize(maxPoolSize);
pool.setKeepAliveSeconds(keepAliveTime);
pool.setQueueCapacity(queueCapacity);
pool.setCorePoolSize(CORE_POOL_SIZE);
pool.setMaxPoolSize(MAX_POOL_SIZE);
pool.setKeepAliveSeconds(KEEP_ALIVE_TIME);
pool.setQueueCapacity(QUEUE_CAPACITY);
pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
pool.initialize();
......
......@@ -9,14 +9,18 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.consts.Constant;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.event.CreateForwardClientEvent;
import iot.sixiang.license.event.DeviceClientInactiveEvent;
import iot.sixiang.license.event.DeviceClientLicenseEvent;
import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientRequestEvent;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.model.dto.ReportErrorMsgDTO;
import iot.sixiang.license.model.vo.UserInfoVO;
import iot.sixiang.license.service.PmsUseService;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.service.TerminalDeviceService;
import iot.sixiang.license.third_lib.LibHelper;
import iot.sixiang.license.util.CommonUtil;
......@@ -28,6 +32,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.List;
@Component
......@@ -41,6 +46,10 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
private PmsUseService pmsUseService;
@Resource
TerminalDeviceService terminalDeviceService;
@Resource
ReportService reportService;
@Resource
UserMapper userMapper;
public DeviceServerHandler() {
super();
......@@ -172,6 +181,10 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
eventPublisher.publishEvent(event);
} else {
pmsUseService.createFailUseLog(sn, "鉴权失败");
UserInfoVO user = userMapper.getUserBySn(sn);
Report report = Report.builder().category(0).type(6).sn(sn).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report);
}
return license;
}
......
......@@ -3,6 +3,7 @@ package iot.sixiang.license.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
......@@ -13,6 +14,7 @@ import java.util.Date;
* Description
*/
@Data
@Builder
public class Report {
@ApiModelProperty("id")
@TableId(type = IdType.AUTO)
......
......@@ -2,16 +2,22 @@ package iot.sixiang.license.event;
import com.alibaba.fastjson.JSONObject;
import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.SamInfo;
import iot.sixiang.license.model.SamMonitor;
import iot.sixiang.license.model.vo.UserInfoVO;
import iot.sixiang.license.operate.OperateManager;
import iot.sixiang.license.service.AlarmService;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
......@@ -23,9 +29,12 @@ import java.util.List;
public class OperateSAMStatusResponseEventHandler {
@Autowired
public OperateManager operateManager;
@Autowired
private AlarmService alarmService;
@Resource
UserMapper userMapper;
@Resource
ReportService reportService;
public OperateSAMStatusResponseEventHandler() {
}
......@@ -51,6 +60,10 @@ public class OperateSAMStatusResponseEventHandler {
String title = "SAM故障";
String content = "index为" + index + "的SAM发生故障";
alarmService.addAlarm(typeId,title,content);
UserInfoVO user = userMapper.getRootAccount();
Report report = Report.builder().category(1).type(1).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report);
}
}
SamMonitor samMonitor = new SamMonitor();
......@@ -63,6 +76,10 @@ public class OperateSAMStatusResponseEventHandler {
String title = "SAM不足";
String content = "当前在线客户端已超过70%";
alarmService.addAlarm(typeId,title,content);
UserInfoVO user = userMapper.getRootAccount();
Report report = Report.builder().category(1).type(2).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report);
}
}
}
......@@ -4,17 +4,23 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.event.DeviceClientBeForcedOfflineEvent;
import iot.sixiang.license.event.DeviceClientLicenseEvent;
import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientConnectEvent;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.model.vo.UserInfoVO;
import iot.sixiang.license.net.BaseConnectionListener;
import iot.sixiang.license.operate.OperateManager;
import iot.sixiang.license.service.AlarmService;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
@Slf4j
public class ForwardConnectionListener extends BaseConnectionListener {
private DeviceManager deviceManager;
......@@ -34,7 +40,13 @@ public class ForwardConnectionListener extends BaseConnectionListener {
String title = "连接服器失败";
String content = "连接服务器:" + this.host + ":" + this.port + "失败";
alarmService.addAlarm(typeId, title, content);
UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
ReportService reportService = SpringUtil.getBean(ReportService.class);
UserInfoVO user = userMapper.getRootAccount();
Report report = Report.builder().category(1).type(3).userName(user.getUserName())
.description("桥接客户端,连接服务器" + this.host + ":" + this.port + "失败")
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report);
// forward client连接失败,则强制踢掉设备客户端
DeviceClientBeForcedOfflineEvent deviceClientBeForcedOfflineEvent = new DeviceClientBeForcedOfflineEvent();
deviceClientBeForcedOfflineEvent.setSn(this.sn);
......
......@@ -3,6 +3,7 @@ package iot.sixiang.license.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import iot.sixiang.license.entity.User;
import iot.sixiang.license.model.vo.UserChildIdVO;
import iot.sixiang.license.model.vo.UserInfoVO;
import iot.sixiang.license.model.vo.UserVo;
import java.util.List;
......@@ -31,4 +32,8 @@ public interface UserMapper extends BaseMapper<User> {
User getUserById(int userId);
List<UserChildIdVO> getChildUser(int userId);
UserInfoVO getRootAccount();
UserInfoVO getUserBySn(String sn);
}
package iot.sixiang.license.model.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* Created by m33
* Date 2023/2/2 15:03
* Description
*/
@Data
public class UserInfoVO {
@ApiModelProperty("用户号")
private String userName;
@ApiModelProperty("公司名")
private String company;
}
......@@ -3,13 +3,19 @@ package iot.sixiang.license.operate;
import io.netty.channel.ChannelFuture;
import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.OperateSAMStatusRequestEvent;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.vo.UserInfoVO;
import iot.sixiang.license.net.BaseConnectionListener;
import iot.sixiang.license.service.AlarmService;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.util.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
@Slf4j
public class OperateConnectionListener extends BaseConnectionListener {
......@@ -25,6 +31,13 @@ public class OperateConnectionListener extends BaseConnectionListener {
String title = "连接服器失败";
String content = "连接服务器:" + this.host + ":" + this.port + "失败";
alarmService.addAlarm(typeId, title, content);
UserMapper userMapper = SpringUtil.getBean(UserMapper.class);
ReportService reportService = SpringUtil.getBean(ReportService.class);
UserInfoVO user = userMapper.getRootAccount();
Report report = Report.builder().category(1).type(3).userName(user.getUserName())
.description("运维客户端,连接服务器" + this.host + ":" + this.port + "失败")
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report);
} else {
log.info("运维客户端,连接服务器成功:{},{}", this.host, this.port);
//TODO 查询SAM状态
......
......@@ -21,4 +21,6 @@ public interface PmsUseService {
boolean reportErrorMsg(ReportErrorMsgDTO reportErrorMsgDTO);
boolean deletePmsUseLogById(Integer id);
PmsUseLog getPmsUseLogById(Integer id);
}
package iot.sixiang.license.service;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.model.BaseResult;
import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.model.dto.ReportDTO;
import iot.sixiang.license.model.vo.ReportVO;
......@@ -12,4 +14,6 @@ import iot.sixiang.license.model.vo.ReportVO;
public interface ReportService {
PageResult<ReportVO> getReportList(ReportDTO reportDTO);
void reportToEmail(Report report);
}
......@@ -107,6 +107,11 @@ public class PmsUseServiceImpl implements PmsUseService {
return res > 0;
}
@Override
public PmsUseLog getPmsUseLogById(Integer id) {
return pmsUseLogMapper.selectById(id);
}
private PmsUseLog getPmsUseLog(String sn) {
PmsUseLog pmsUseLog = new PmsUseLog();
Date date = new Date();
......
package iot.sixiang.license.service.impl;
import cn.hutool.core.lang.Validator;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.entity.ReportType;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.jwt.UserUtils;
import iot.sixiang.license.mapper.ReportMapper;
import iot.sixiang.license.mapper.ReportTypeMapper;
import iot.sixiang.license.model.PageResult;
import iot.sixiang.license.model.dto.ReportDTO;
import iot.sixiang.license.model.vo.ReportVO;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.util.CommonUtil;
import iot.sixiang.license.util.EmailUtils;
import iot.sixiang.license.util.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
......@@ -25,6 +32,10 @@ import java.util.stream.Collectors;
public class ReportServiceImpl implements ReportService {
@Resource
ReportMapper reportMapper;
@Resource
EmailUtils emailUtils;
@Resource
ReportTypeMapper reportTypeMapper;
@Override
public PageResult<ReportVO> getReportList(ReportDTO reportDTO) {
......@@ -58,4 +69,31 @@ public class ReportServiceImpl implements ReportService {
}
return new PageResult(200, "查找成功", pageNo, pages, total, result);
}
@Async("mailExecutor")
@Override
public void reportToEmail(Report report) {
if (!Validator.isEmail(report.getUserName())) {
throw new IotLicenseException(403, "报警失败,无效的邮箱账号");
}
reportMapper.insert(report);
Integer type = report.getType();
ReportType reportType = reportTypeMapper.selectById(type);
if (reportType == null) {
throw new IotLicenseException(403, "报警失败,无效的报警类型");
}
Integer category = report.getCategory();
String content = "";
if (category == 1) {
content = "【实名制身份验证云平台】系统报警!报警时间:" + CommonUtil.getSystemTime() + ",报警事件:" + reportType.getDescription()
+ ",报警用户:" + report.getUserName() + ",公司名称:" + report.getUserCompany() + ",请核实!";
} else if (category == 0) {
content = "【实名制身份验证云平台】设备报警!报警时间:" + CommonUtil.getSystemTime() + ",报警事件:" + reportType.getDescription()
+ ",设备SN:" + report.getSn() + ",报警用户:" + report.getUserName() + ",公司名称:" + report.getUserCompany()
+ ",请核实!";
} else {
throw new IotLicenseException(403, "报警失败,无效的报警大类");
}
emailUtils.sendSimpleMail(report.getUserName(), "实名制身份验证云平台", content);
}
}
......@@ -2,18 +2,19 @@ package iot.sixiang.license.service.impl;
import iot.sixiang.license.auth.AuthManager;
import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.entity.PmsUseLog;
import iot.sixiang.license.entity.Report;
import iot.sixiang.license.handler.IotLicenseException;
import iot.sixiang.license.jwt.JwtUtil;
import iot.sixiang.license.jwt.LoginUser;
import iot.sixiang.license.mapper.UserMapper;
import iot.sixiang.license.model.BaseResult;
import iot.sixiang.license.model.ResResult;
import iot.sixiang.license.model.dto.GetTerminalDeviceTokenDTO;
import iot.sixiang.license.model.dto.PlatFormBindDTO;
import iot.sixiang.license.model.dto.ReportErrorMsgDTO;
import iot.sixiang.license.model.dto.TerminalDevieBindDTO;
import iot.sixiang.license.model.dto.TerminalDevieUnBindDTO;
import iot.sixiang.license.model.dto.*;
import iot.sixiang.license.model.vo.UserInfoVO;
import iot.sixiang.license.service.DeviceService;
import iot.sixiang.license.service.PmsUseService;
import iot.sixiang.license.service.ReportService;
import iot.sixiang.license.service.TerminalDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
......@@ -21,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Slf4j
......@@ -34,6 +36,10 @@ public class TerminalDeviceServiceImpl implements TerminalDeviceService {
private PmsUseService pmsUseService;
@Resource
private DeviceManager deviceManager;
@Resource
UserMapper userMapper;
@Resource
ReportService reportService;
@Override
public ResResult getToken(GetTerminalDeviceTokenDTO getTerminalDeviceTokenDTO) {
......@@ -76,6 +82,23 @@ public class TerminalDeviceServiceImpl implements TerminalDeviceService {
} else if ("-1".equals(reportErrorMsgDTO.getErrorCode())) {
pmsUseService.deletePmsUseLogById(id);
} else {
PmsUseLog pmsUseLog = pmsUseService.getPmsUseLogById(reportErrorMsgDTO.getId());
if (pmsUseLog == null || StringUtils.isEmpty(pmsUseLog.getSn())) {
throw new IotLicenseException(403, "添加报警信息失败:查无SN");
}
UserInfoVO user = userMapper.getUserBySn(pmsUseLog.getSn());
int code = Integer.parseInt(reportErrorMsgDTO.getErrorCode());
int type = 0;
if (code == 1) {
type = 5;
} else if (code == 2) {
type = 4;
} else if (code == 3) {
type = 7;
}
Report report = Report.builder().category(0).type(type).sn(pmsUseLog.getSn()).userName(user.getUserName())
.userCompany(user.getCompany()).createTime(new Date()).updateTime(new Date()).build();
reportService.reportToEmail(report);
boolean res = pmsUseService.reportErrorMsg(reportErrorMsgDTO);
if (!res) {
return BaseResult.failed();
......
......@@ -52,4 +52,15 @@
select user_id, parent, `level` from `user` where parent = #{userId}
</select>
<select id="getUserBySn" resultType="iot.sixiang.license.model.vo.UserInfoVO">
SELECT `user`.user_name, `user`.company FROM user
INNER JOIN apply on user.user_id = apply.user_id
INNER JOIN device on device.app_id = apply.app_id
WHERE device.sn = #{sn}
</select>
<select id="getRootAccount" resultType="iot.sixiang.license.model.vo.UserInfoVO">
SELECT user_name, company FROM `user` WHERE parent IS NULL AND `level` = 1 LIMIT 1
</select>
</mapper>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment