审计日志相关代码提交
This commit is contained in:
26
pom.xml
26
pom.xml
@@ -37,27 +37,27 @@
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
<properties>
|
||||
<!--中间件目标地址-->
|
||||
<middle.server.url>192.168.1.13</middle.server.url>
|
||||
<!--微服务模块发布地址-->
|
||||
<service.server.url>192.168.1.111</service.server.url>
|
||||
<!--nacos的ip:port-->
|
||||
<nacos.url>192.168.1.14:18848</nacos.url>
|
||||
<nacos.url>${middle.server.url}:18848</nacos.url>
|
||||
<!--服务器发布内容为空-->
|
||||
<!-- <nacos.namespace></nacos.namespace>-->
|
||||
<nacos.namespace>6b3de6af-da16-41c7-a482-58902c408c0d</nacos.namespace>
|
||||
<!-- <nacos.namespace></nacos.namespace>-->
|
||||
<nacos.namespace>fd74182b-1fce-4dba-afa7-2623b0376205</nacos.namespace>
|
||||
<!--sentinel:port-->
|
||||
<!-- <sentinel.url>192.168.1.14:8080</sentinel.url>-->
|
||||
<sentinel.url>192.168.1.139:8080</sentinel.url>
|
||||
<!--微服务发布地址-->
|
||||
<!-- <server.url>192.168.1.14</server.url>-->
|
||||
<server.url>192.168.1.139</server.url>
|
||||
<!-- <sentinel.url>192.168.1.14:8080</sentinel.url>-->
|
||||
<sentinel.url>${middle.server.url}:8080</sentinel.url>
|
||||
<!--网关地址,主要用于配置swagger中认证token-->
|
||||
<!-- <gateway.url>192.168.1.14:10215</gateway.url>-->
|
||||
<gateway.url>192.168.1.139:10215</gateway.url>
|
||||
<gateway.url>${service.server.url}:10215</gateway.url>
|
||||
<!--docker远程守护进程的url-->
|
||||
<docker.url>http://${server.url}:2375</docker.url>
|
||||
<docker.url>http://${middle.server.url}:2375</docker.url>
|
||||
<!--docker远程harbor,url-->
|
||||
<docker.repostory>${server.url}:8090</docker.repostory>
|
||||
<docker.repostory>${middle.server.url}:8090</docker.repostory>
|
||||
<!--docker远程项目仓库名-->
|
||||
<docker.registry.name>njcn</docker.registry.name>
|
||||
<docker.operate>site</docker.operate>
|
||||
<docker.operate>deploy</docker.operate>
|
||||
<java.version>1.8</java.version>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
|
||||
@@ -21,7 +21,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -49,4 +49,8 @@ mybatis-plus:
|
||||
#别名扫描
|
||||
type-aliases-package: com.njcn.user.pojo
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
@@ -64,10 +64,10 @@
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.alibaba</groupId>-->
|
||||
<!-- <artifactId>easyexcel</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.alibaba</groupId>-->
|
||||
<!-- <artifactId>easyexcel</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!--API接口文档-->
|
||||
<dependency>
|
||||
<groupId>com.github.xiaoymin</groupId>
|
||||
@@ -91,6 +91,11 @@
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-annotations</artifactId>
|
||||
</dependency>
|
||||
<!--mqtt相关依赖-->
|
||||
<dependency>
|
||||
<groupId>com.github.tocrhz</groupId>
|
||||
<artifactId>mqtt-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -29,12 +29,26 @@ public class LogInfoDTO implements Serializable {
|
||||
|
||||
private String operateType;
|
||||
|
||||
private String result;
|
||||
/**
|
||||
* 操作结果 0.失败 1.成功
|
||||
*/
|
||||
private Integer result;
|
||||
|
||||
private String level;
|
||||
/**
|
||||
* 严重度 0.普通 1.中等 2.严重
|
||||
*/
|
||||
private Integer level;
|
||||
|
||||
private String type;
|
||||
/**
|
||||
* 事件类型 0.业务事件 1.系统事件
|
||||
*/
|
||||
private Integer type;
|
||||
|
||||
private String serviceName;
|
||||
|
||||
/**
|
||||
* 0 未登录; 1 已登录
|
||||
*/
|
||||
private Integer isLogin;
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.njcn.common.utils;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
@@ -120,6 +121,21 @@ public class PubUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将实体对象转为JSON
|
||||
*
|
||||
* @param object 实体对象
|
||||
*/
|
||||
public static String obj2json(Object object) {
|
||||
try {
|
||||
MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
|
||||
return MAPPER.writeValueAsString(object);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalArgumentException("将将实体对象转为JSON时发生错误:" + object, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断一个数字是否在区间内
|
||||
*
|
||||
|
||||
@@ -2,11 +2,15 @@ package com.njcn.web.service.impl;
|
||||
|
||||
import cn.hutool.core.text.StrFormatter;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.tocrhz.mqtt.publisher.MqttPublisher;
|
||||
import com.njcn.common.config.GeneralInfo;
|
||||
import com.njcn.common.pojo.constant.LogInfo;
|
||||
import com.njcn.common.pojo.dto.LogInfoDTO;
|
||||
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
|
||||
import com.njcn.common.pojo.response.HttpResult;
|
||||
import com.njcn.common.utils.PubUtils;
|
||||
import com.njcn.common.utils.ReflectCommonUtil;
|
||||
import com.njcn.web.service.ILogService;
|
||||
import com.njcn.web.utils.RequestUtil;
|
||||
@@ -46,6 +50,8 @@ public class LogServiceImpl implements ILogService {
|
||||
|
||||
private final GeneralInfo generalInfo;
|
||||
|
||||
private final MqttPublisher publisher;
|
||||
|
||||
/**
|
||||
* 异步记录controller中返回的信息内容
|
||||
*
|
||||
@@ -59,19 +65,21 @@ public class LogServiceImpl implements ILogService {
|
||||
public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) {
|
||||
//处理审计日志
|
||||
String userName;
|
||||
int isLogin = 0;
|
||||
HttpServletRequest httpServletRequest = RequestUtil.getRequest(request);
|
||||
if (UN_LOGIN_METHOD.contains(methodDescribe)) {
|
||||
userName = RequestUtil.getLoginName(httpServletRequest);
|
||||
} else {
|
||||
userName = RequestUtil.getUserNickname(request);
|
||||
isLogin = 1;
|
||||
}
|
||||
String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage();
|
||||
String ip = RequestUtil.getRealIp(request);
|
||||
String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType();
|
||||
String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel();
|
||||
String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod());
|
||||
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, methodDescribe, result, ip, type, level, operateType, generalInfo.getMicroServiceName());
|
||||
System.out.println(logInfoDTO);
|
||||
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin);
|
||||
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -87,8 +95,10 @@ public class LogServiceImpl implements ILogService {
|
||||
public void recodeBusinessExceptionLog(Exception exception, HttpServletRequest request, String message) {
|
||||
LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request);
|
||||
//认证前,获取用户信息
|
||||
int isLogin = 1;
|
||||
if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) {
|
||||
tempLogInfo.setUserName(RequestUtil.getLoginName(request));
|
||||
isLogin = 0;
|
||||
}
|
||||
//根据异常获取method方法
|
||||
Method method = ReflectCommonUtil.getMethod(exception);
|
||||
@@ -101,9 +111,8 @@ public class LogServiceImpl implements ILogService {
|
||||
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
|
||||
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
|
||||
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
|
||||
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), methodDescribe, result, tempLogInfo.getIp(), type, level, operateType, generalInfo.getMicroServiceName());
|
||||
System.out.println(logInfoDTO);
|
||||
|
||||
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin);
|
||||
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,10 +138,37 @@ public class LogServiceImpl implements ILogService {
|
||||
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
|
||||
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
|
||||
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
|
||||
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, methodDescribe, result, ip, type, level, operateType, generalInfo.getMicroServiceName());
|
||||
System.out.println(logInfoDTO);
|
||||
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), 0);
|
||||
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 严重度 文字 转 数字
|
||||
*/
|
||||
private Integer levelStringToNumber(String level) {
|
||||
switch (level) {
|
||||
case "中等":
|
||||
return 1;
|
||||
case "严重":
|
||||
return 2;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 严重度 数字 转 文字
|
||||
*/
|
||||
private String levelNumberToString(Integer level) {
|
||||
switch (level) {
|
||||
case 1:
|
||||
return "中等";
|
||||
case 2:
|
||||
return "严重";
|
||||
default:
|
||||
return "普通";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<module>common-autocode</module>
|
||||
<module>common-influxdb</module>
|
||||
<module>common-poi</module>
|
||||
<!-- <module>common-echarts</module>-->
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -21,7 +21,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -54,3 +54,11 @@ logging:
|
||||
mybatis-plus:
|
||||
#别名扫描
|
||||
type-aliases-package: com.njcn.device.pojo
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
|
||||
@@ -54,10 +54,7 @@
|
||||
<version>${project.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.tocrhz</groupId>
|
||||
<artifactId>mqtt-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jetbrains</groupId>
|
||||
<artifactId>annotations</artifactId>
|
||||
|
||||
@@ -47,8 +47,9 @@ mybatis-plus:
|
||||
#别名扫描
|
||||
type-aliases-package: com.njcn.energy.pojo
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://192.168.1.14:1883
|
||||
client-id: energy_cdf
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@_cdf
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
password: njcnpqs
|
||||
@@ -20,6 +20,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -47,6 +48,11 @@ mybatis-plus:
|
||||
type-aliases-package: com.njcn.event.pojo
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -133,9 +133,11 @@ whitelist:
|
||||
- /quality-boot/**
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -48,7 +48,11 @@ mybatis-plus:
|
||||
type-aliases-package: com.njcn.harmonic.pojo
|
||||
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -118,4 +119,3 @@ management:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ xxl:
|
||||
#执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。
|
||||
address:
|
||||
#执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务";
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
#执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口;
|
||||
port: 10219
|
||||
#执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径;
|
||||
|
||||
@@ -21,6 +21,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -50,7 +51,7 @@ mybatis-plus:
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://192.168.1.18:1883
|
||||
client-id: energy
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
@@ -68,4 +68,5 @@ public class UserLog extends BaseEntity {
|
||||
*/
|
||||
private Integer state;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -49,11 +49,8 @@ public class UserLogController extends BaseController {
|
||||
@ApiImplicitParam(name = "logInfoDTO", value = "插入日志参数", required = true)
|
||||
public HttpResult addUserLog(@RequestBody LogInfoDTO logInfoDTO) {
|
||||
String methodDescribe = getMethodDescribe("addUserLog");
|
||||
boolean res = userLogService.addUserLog(logInfoDTO);
|
||||
if (res) {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,null,methodDescribe);
|
||||
}
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL,null,methodDescribe);
|
||||
userLogService.addUserLog(logInfoDTO);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.njcn.system.handler;
|
||||
|
||||
import com.github.tocrhz.mqtt.annotation.MqttSubscribe;
|
||||
import com.github.tocrhz.mqtt.annotation.Payload;
|
||||
import com.njcn.common.pojo.dto.LogInfoDTO;
|
||||
import com.njcn.common.utils.PubUtils;
|
||||
import com.njcn.system.service.IUserLogService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
* @version 1.0.0
|
||||
* @date 2022年07月13日 13:35
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class MqttMessageHandler {
|
||||
|
||||
private final IUserLogService userLogService;
|
||||
|
||||
/**
|
||||
* 订阅审计日志的记录,并进行入库操作
|
||||
*/
|
||||
@MqttSubscribe(value = "/userLog")
|
||||
public void subUserLog(String topic, MqttMessage message, @Payload String payload) {
|
||||
LogInfoDTO logInfoDTO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class);
|
||||
userLogService.addUserLog(logInfoDTO);
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -47,3 +47,10 @@ logging:
|
||||
mybatis-plus:
|
||||
#别名扫描
|
||||
type-aliases-package: com.njcn.system.pojo
|
||||
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
@@ -21,7 +21,7 @@ spring:
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
ip: @server.url@
|
||||
ip: @service.server.url@
|
||||
server-addr: @nacos.url@
|
||||
namespace: @nacos.namespace@
|
||||
config:
|
||||
@@ -48,3 +48,9 @@ logging:
|
||||
mybatis-plus:
|
||||
#别名扫描
|
||||
type-aliases-package: com.njcn.user.pojo
|
||||
|
||||
mqtt:
|
||||
uri: tcp://@middle.server.url@:1883
|
||||
client-id: @artifactId@
|
||||
username: t_user
|
||||
password: njcnpqs
|
||||
|
||||
Reference in New Issue
Block a user