审计日志入库完成

This commit is contained in:
2022-07-14 19:27:23 +08:00
parent f58dda8534
commit e93a3c1e08
6 changed files with 120 additions and 73 deletions

View File

@@ -66,6 +66,7 @@ public class UserTokenService {
JSONObject accessJson = JSONUtil.parseObj(accessJwsObject.getPayload().toString()); JSONObject accessJson = JSONUtil.parseObj(accessJwsObject.getPayload().toString());
String userIndex = accessJson.getStr(SecurityConstants.USER_INDEX_KEY); String userIndex = accessJson.getStr(SecurityConstants.USER_INDEX_KEY);
String nickName = accessJson.getStr(SecurityConstants.USER_NICKNAME_KEY); String nickName = accessJson.getStr(SecurityConstants.USER_NICKNAME_KEY);
String loginName = accessJson.getStr(SecurityConstants.USER_NAME_KEY);
//查询是否有在线的当前用户 //查询是否有在线的当前用户
String onlineUserKey = SecurityConstants.TOKEN_ONLINE_PREFIX + userIndex; String onlineUserKey = SecurityConstants.TOKEN_ONLINE_PREFIX + userIndex;
Object onlineTokenInfoOld = redisUtil.getObjectByKey(onlineUserKey); Object onlineTokenInfoOld = redisUtil.getObjectByKey(onlineUserKey);
@@ -103,7 +104,7 @@ public class UserTokenService {
redisUtil.saveByKeyWithExpire(onlineUserKey, userTokenInfo, refreshLifeTime.plusMinutes(5L).toEpochSecond(ZoneOffset.of("+8")) - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"))); redisUtil.saveByKeyWithExpire(onlineUserKey, userTokenInfo, refreshLifeTime.plusMinutes(5L).toEpochSecond(ZoneOffset.of("+8")) - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
//记录成功登录后的信息 //记录成功登录后的信息
LogInfoDTO logInfoDTO = new LogInfoDTO(nickName, ip, "登录认证", OperateType.AUTHENTICATE, 1, "",0, 1, generalInfo.getMicroServiceName(), userIndex); LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, nickName, ip, "登录认证", OperateType.AUTHENTICATE, 1, "", 0, 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
} }

View File

@@ -21,8 +21,14 @@ public class LogInfoDTO implements Serializable {
/** /**
* 登录名 * 登录名
*/ */
private String loginName;
/**
* 用户已登录:用户名
*/
private String userName; private String userName;
private String ip; private String ip;
private String operate; private String operate;

View File

@@ -57,14 +57,17 @@ public class LogServiceImpl implements ILogService {
@Async("asyncExecutor") @Async("asyncExecutor")
public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) { public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) {
//处理审计日志 //处理审计日志
String loginName;
String userName; String userName;
String userIndex; String userIndex;
HttpServletRequest httpServletRequest = RequestUtil.getRequest(request); HttpServletRequest httpServletRequest = RequestUtil.getRequest(request);
if (UN_LOGIN_METHOD.contains(methodDescribe)) { if (UN_LOGIN_METHOD.contains(methodDescribe)) {
userName = RequestUtil.getLoginName(httpServletRequest); loginName = RequestUtil.getLoginName(httpServletRequest);
userName = "";
userIndex = userName; userIndex = userName;
} else { } else {
userName = RequestUtil.getLoginName(request); loginName = RequestUtil.getLoginNameByPayload(request);
userName = RequestUtil.getUserNickname(request);
userIndex = RequestUtil.getUserIndex(request); userIndex = RequestUtil.getUserIndex(request);
} }
String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage(); String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage();
@@ -72,7 +75,7 @@ public class LogServiceImpl implements ILogService {
String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType(); String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel(); String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod()); String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod());
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "",levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, "", levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false);
} }
@@ -90,9 +93,10 @@ public class LogServiceImpl implements ILogService {
LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request); LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request);
//认证前,获取用户信息 //认证前,获取用户信息
String userIndex; String userIndex;
if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) { if (Objects.equals(tempLogInfo.getLoginName(), LogInfo.UNKNOWN_USER)) {
tempLogInfo.setUserName(RequestUtil.getLoginName(request)); tempLogInfo.setLoginName(RequestUtil.getLoginName(request));
userIndex = tempLogInfo.getUserName(); tempLogInfo.setUserName("");
userIndex = tempLogInfo.getLoginName();
} else { } else {
userIndex = RequestUtil.getUserIndex(request); userIndex = RequestUtil.getUserIndex(request);
} }
@@ -106,7 +110,7 @@ public class LogServiceImpl implements ILogService {
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex); LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getLoginName(), tempLogInfo.getUserName(), tempLogInfo.getIp(), ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1, message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userIndex);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
} }
@@ -116,11 +120,11 @@ public class LogServiceImpl implements ILogService {
* @param exception 异常数据 * @param exception 异常数据
* @param request 请求信息 * @param request 请求信息
* @param message 信息 * @param message 信息
* @param userName 用户 * @param loginName 登录
*/ */
@Override @Override
@Async("asyncExecutor") @Async("asyncExecutor")
public void recodeAuthExceptionLog(Exception exception, HttpServletRequest request, String message, String userName) { public void recodeAuthExceptionLog(Exception exception, HttpServletRequest request, String message, String loginName) {
//根据异常获取method方法 //根据异常获取method方法
Method method = ReflectCommonUtil.getMethod(exception); Method method = ReflectCommonUtil.getMethod(exception);
if (exception instanceof MethodArgumentNotValidException) { if (exception instanceof MethodArgumentNotValidException) {
@@ -132,7 +136,7 @@ public class LogServiceImpl implements ILogService {
String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType();
String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel();
String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method);
LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1,message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), userName); LogInfoDTO logInfoDTO = new LogInfoDTO(loginName, "", ip, ReflectCommonUtil.getMethodDescribeByMethod(method), operateType, result.equalsIgnoreCase("失败") ? 0 : 1, message, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), loginName);
publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false);
} }

View File

@@ -331,6 +331,19 @@ public class RequestUtil {
return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName; return StrUtil.isBlank(loginName) ? LogInfo.UNKNOWN_USER : loginName;
} }
/**
* ServerHttpRequest获取在网关中存储的用户昵称
*/
public static String getLoginNameByPayload(ServerHttpRequest request) {
String loginName = LogInfo.UNKNOWN_USER;
JSONObject jwtPayload = getJwtPayload(request);
if (Objects.nonNull(jwtPayload)) {
String loginNameTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY);
loginName = StrUtil.isBlank(loginNameTemp) ? LogInfo.UNKNOWN_USER : loginNameTemp;
}
return loginName;
}
/** /**
* ServerHttpRequest获取用户登录名 * ServerHttpRequest获取用户登录名
*/ */
@@ -376,11 +389,15 @@ public class RequestUtil {
LogInfoDTO temp = new LogInfoDTO(); LogInfoDTO temp = new LogInfoDTO();
temp.setIp(StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip); temp.setIp(StrUtil.isBlank(ip) ? LogInfo.UNKNOWN_IP : ip);
String username = LogInfo.UNKNOWN_USER; String username = LogInfo.UNKNOWN_USER;
String loginName = LogInfo.UNKNOWN_USER;
JSONObject jwtPayload = getJwtPayload(request); JSONObject jwtPayload = getJwtPayload(request);
if (Objects.nonNull(jwtPayload)) { if (Objects.nonNull(jwtPayload)) {
String userSignTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY); String loginNameTemp = jwtPayload.getString(SecurityConstants.USER_NAME_KEY);
username = StrUtil.isBlank(userSignTemp) ? LogInfo.UNKNOWN_USER : userSignTemp; loginName = StrUtil.isBlank(loginNameTemp) ? LogInfo.UNKNOWN_USER : loginNameTemp;
String userNameTemp = jwtPayload.getString(SecurityConstants.USER_NICKNAME_KEY);
username = StrUtil.isBlank(userNameTemp) ? LogInfo.UNKNOWN_USER : userNameTemp;
} }
temp.setLoginName(loginName);
temp.setUserName(username); temp.setUserName(username);
return temp; return temp;
} }

View File

@@ -174,7 +174,7 @@ public class TransientServiceImpl implements TransientService {
// waveDataVO.setName("变电站名称: "+ substation +" 监测点名称: "+ name +" 发生时刻: "+ timeId +" 暂降幅值: "+ v +"% 持续时间: "+ persistTime +"s"); // waveDataVO.setName("变电站名称: "+ substation +" 监测点名称: "+ name +" 发生时刻: "+ timeId +" 暂降幅值: "+ v +"% 持续时间: "+ persistTime +"s");
// waveDataVO.setTargetName("相电压有效值"); // waveDataVO.setTargetName("相电压有效值");
AnalyWave analyWave = new AnalyWave(); AnalyWave analyWave = new AnalyWave();
WaveDataDTO comtrade = analyWave.getComtrade(eventBaseConfig.getWavePath()+"\\"+ip+"\\"+waveName+".CFG", 1); WaveDataDTO comtrade = analyWave.getComtrade(eventBaseConfig.getWavePath() + File.separator + ip + File.separator + waveName + ".CFG", 1);
if (Objects.isNull(comtrade.getComtradeCfgDTO())) { if (Objects.isNull(comtrade.getComtradeCfgDTO())) {
throw new BusinessException(EventResponseEnum.ANALYSEWAVE_NOT_FOUND); throw new BusinessException(EventResponseEnum.ANALYSEWAVE_NOT_FOUND);
} }
@@ -229,7 +229,9 @@ public class TransientServiceImpl implements TransientService {
return response; return response;
} }
/**复制保存文件 */ /**
* 复制保存文件
*/
public void copyTempData(List<String> timeId, List<String> lineId) throws IOException { public void copyTempData(List<String> timeId, List<String> lineId) throws IOException {
for (int i = 0; i < timeId.size(); i++) { for (int i = 0; i < timeId.size(); i++) {
//根据监测点id获取监测点详情 //根据监测点id获取监测点详情
@@ -256,6 +258,7 @@ public class TransientServiceImpl implements TransientService {
writeFile(srcDATFile, dat); writeFile(srcDATFile, dat);
} }
} }
//读写文件 //读写文件
public static void writeFile(File wave, File temp) throws IOException { public static void writeFile(File wave, File temp) throws IOException {
FileInputStream fis = new FileInputStream(wave); FileInputStream fis = new FileInputStream(wave);
@@ -276,6 +279,7 @@ public class TransientServiceImpl implements TransientService {
/** /**
* 将文件夹及文件夹包含的内容压缩成zip文件 * 将文件夹及文件夹包含的内容压缩成zip文件
* (为了解决中文乱码的问题ZipOutputStream用org.apache.tools.zip.*) * (为了解决中文乱码的问题ZipOutputStream用org.apache.tools.zip.*)
*
* @param inputFile 源文件(夹) * @param inputFile 源文件(夹)
* @return File 压缩后的文件(路径在源文件的文件夹路径下) * @return File 压缩后的文件(路径在源文件的文件夹路径下)
*/ */
@@ -312,8 +316,10 @@ public class TransientServiceImpl implements TransientService {
} }
return zipFile; return zipFile;
} }
/** /**
* 压缩文件或文件夹 (ZipEntry 使用org.apache.tools.zip.*,不要用 java.util.zip.*) * 压缩文件或文件夹 (ZipEntry 使用org.apache.tools.zip.*,不要用 java.util.zip.*)
*
* @param zos zip输出流 * @param zos zip输出流
* @param sourceFile 源文件 * @param sourceFile 源文件
* @param baseName 父路径 * @param baseName 父路径
@@ -358,8 +364,10 @@ public class TransientServiceImpl implements TransientService {
} }
} }
} }
/** /**
* 流拷贝 * 流拷贝
*
* @param in 输入流 * @param in 输入流
* @param out 输出流 * @param out 输出流
* @throws IOException * @throws IOException
@@ -380,7 +388,10 @@ public class TransientServiceImpl implements TransientService {
//================================================================================================================= //=================================================================================================================
/**处理文件路径和指定压缩包位置 */
/**
* 处理文件路径和指定压缩包位置
*/
public static String downloadWaveMoreFile(List<String> pathList) { public static String downloadWaveMoreFile(List<String> pathList) {
List<File> list = new ArrayList<>(); List<File> list = new ArrayList<>();
for (String string : pathList) { for (String string : pathList) {
@@ -401,6 +412,7 @@ public class TransientServiceImpl implements TransientService {
/** /**
* 压缩多个文件成一个zip文件 * 压缩多个文件成一个zip文件
*
* @param srcfile源文件列表 * @param srcfile源文件列表
* @param zipfile压缩后的文件 * @param zipfile压缩后的文件
*/ */
@@ -426,7 +438,9 @@ public class TransientServiceImpl implements TransientService {
} }
} }
/**删除文件(夹)和空文件夹 */ /**
* 删除文件(夹)和空文件夹
*/
private void deleteDirectoryLegacyIO(File file) { private void deleteDirectoryLegacyIO(File file) {
File[] list = file.listFiles(); //无法做到list多层文件夹数据 File[] list = file.listFiles(); //无法做到list多层文件夹数据
if (list != null) { if (list != null) {
@@ -443,6 +457,7 @@ public class TransientServiceImpl implements TransientService {
/** /**
* 删除文件 * 删除文件
*
* @param filePathAndName 指定得路径 * @param filePathAndName 指定得路径
*/ */
public static void delFile(String filePathAndName) { public static void delFile(String filePathAndName) {

View File

@@ -22,9 +22,13 @@ public class UserLog extends BaseEntity {
*/ */
private String id; private String id;
/**
* 登录名
*/
private String loginName;
/** /**
* 用户已登录:用户名 * 用户已登录:用户名
* 用户未登录:登录名
*/ */
private String userName; private String userName;