Browse Source

Configure Continuous Integration ....

master
zhangqiyang 7 months ago
parent
commit
dfb94300b4
  1. 12
      modules/test/src/main/resources/views/actionGroup.xml
  2. 4
      modules/test/src/main/resources/views/menu.xml
  3. 39
      src/main/java/com/system/log/controller/LogController.java
  4. 354
      src/main/java/com/system/log/service/LogService.java
  5. 23
      src/main/resources/application.properties

12
modules/test/src/main/resources/views/actionGroup.xml

@ -45,4 +45,16 @@
<action-view name="action.view.SystemManageUserLogBackup.Log" model="Log.LogConfig" title="系统管理员日志备份"> <action-view name="action.view.SystemManageUserLogBackup.Log" model="Log.LogConfig" title="系统管理员日志备份">
<view name="SystemManageUserBackup-grid" type="grid"/> <view name="SystemManageUserBackup-grid" type="grid"/>
</action-view> </action-view>
<action-method name="action.method.system.logManage.systemLog" model="Log.LogEntity">
<call method="getFileZip" class="com.system.log.controller.LogController"/>
</action-method>
<action-view name="action.view.selectSystemLog.Log" model="Log.LogEntity" title="系统运行日志查询">
<view name="LogWeb/index.html" type="html"/>
</action-view>
<action-view name="action.view.SecurityUser.LogConfig" model="Log.LogConfig" title="业务用户日志配置">
<view name="LogBackup/index.html" type="html"/>
</action-view>
</object-views> </object-views>

4
modules/test/src/main/resources/views/menu.xml

@ -5,6 +5,6 @@
<menuitem parent="menu-LogManage" title="安全审计员操作日志" left="true" name="menu-SecurityAuditUser-Log" action="action.view.SecurityAuditUser.Log"/> <menuitem parent="menu-LogManage" title="安全审计员操作日志" left="true" name="menu-SecurityAuditUser-Log" action="action.view.SecurityAuditUser.Log"/>
<menuitem parent="menu-LogManage" title="系统管理员操作日志" left="true" name="menu-SystemManageUser-Log" action="action.view.SystemManageUser.Log"/> <menuitem parent="menu-LogManage" title="系统管理员操作日志" left="true" name="menu-SystemManageUser-Log" action="action.view.SystemManageUser.Log"/>
<menuitem parent="menu-LogManage" title="安全保密员操作日志" left="true" name="menu-SecurityUser-Log" action="action.view.SecurityUser.Log"/> <menuitem parent="menu-LogManage" title="安全保密员操作日志" left="true" name="menu-SecurityUser-Log" action="action.view.SecurityUser.Log"/>
<menuitem parent="menu-LogManage" title="系统运行日志" left="true" name="menu-System-Log"/> <menuitem parent="menu-LogManage" title="系统运行日志" left="true" name="menu-System-Log" action="action.view.selectSystemLog.Log"/>
<menuitem parent="menu-LogManage" title="日志配置" left="true" name="menu-Config-Log"/> <menuitem parent="menu-LogManage" title="日志配置" left="true" name="menu-Config-Log" action="action.view.SecurityUser.LogConfig"/>
</object-views> </object-views>

39
src/main/java/com/system/log/controller/LogController.java

@ -60,14 +60,41 @@ public class LogController extends JpaSupport {
FileUtils.downloadFile(response, file); FileUtils.downloadFile(response, file);
} }
// @GET // 导出操作日志
// @Path("/getFile")
// public JSONObject getFileChange() {
// return logService.getFileChange();
// }
public void exportLog(ActionRequest request, ActionResponse response) throws IOException { public void exportLog(ActionRequest request, ActionResponse response) throws IOException {
File file = logService.exportLog(request, response); File file = logService.exportLog(request, response);
FileUtils.downloadFile(response, file); FileUtils.downloadFile(response, file);
} }
// 日志配置接口
/*
* threshold 在线日志存储阈值
* backupCycle 备份周期
* backupStartTime 备份开始日期
*/
@POST
@Path("/setLogConfig")
public JSONObject setLogConfig(JSONObject jsonObject) {
return logService.setLogConfig(
jsonObject.getInteger("threshold"),
jsonObject.getInteger("backupCycle"),
jsonObject.getString("backupStartTime"));
}
// 日志配置查询接口
@GET
@Path("/selectLogConfig")
public JSONObject selectLogConfig() {
return logService.selectLogConfig();
}
// 删除系统管理员操作日志备份
public void deleteSystemManageUserBackup(ActionRequest request, ActionResponse response) {
logService.deleteSystemManageUserBackup(request, response);
}
// 重新备份系统管理员操作日志
public void secondBackup(ActionRequest request, ActionResponse response) {
logService.secondBackup(request, response);
}
} }

354
src/main/java/com/system/log/service/LogService.java

@ -1,20 +1,28 @@
package com.system.log.service; package com.system.log.service;
import Log.LogBackup;
import Log.LogConfig;
import Log.LogEntity; import Log.LogEntity;
import Log.repo.LogBackupRepository;
import Log.repo.LogConfigRepository;
import Log.repo.LogEntityRepository; import Log.repo.LogEntityRepository;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import com.hypaas.db.JPA;
import com.hypaas.rpc.ActionRequest; import com.hypaas.rpc.ActionRequest;
import com.hypaas.rpc.ActionResponse; import com.hypaas.rpc.ActionResponse;
import com.system.log.LogTypeEnum; import com.system.log.LogTypeEnum;
import com.system.log.vo.LogRecordVO; import com.system.log.vo.LogRecordVO;
import java.io.*; import java.io.*;
import java.text.DecimalFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import module.LogStatus;
import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.ZipParameters;
@ -29,6 +37,10 @@ public class LogService {
@Inject LogEntityRepository logEntityRepository; @Inject LogEntityRepository logEntityRepository;
@Inject LogConfigRepository logConfigRepository;
@Inject LogBackupRepository logBackupRepository;
public JSONObject getDirContent(String logType, String startTime, String endTime, String keyWord) public JSONObject getDirContent(String logType, String startTime, String endTime, String keyWord)
throws IOException { throws IOException {
// 定义集合,存放文件名称 // 定义集合,存放文件名称
@ -138,7 +150,7 @@ public class LogService {
} }
// 输入开始时间与结束时间获得对应的标准毫秒数,方便筛选每个日志文件中的数据,必须以"yyyy-MM-dd HH:mm:ss"的形式输入 // 输入开始时间与结束时间获得对应的标准毫秒数,方便筛选每个日志文件中的数据,必须以"yyyy-MM-dd HH:mm:ss"的形式输入
private long getLongTime(String time) { public long getLongTime(String time) {
Date date; Date date;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try { try {
@ -391,7 +403,7 @@ public class LogService {
return exportLogExcelZip(selectResult, password); return exportLogExcelZip(selectResult, password);
} }
private long getSqlLongTime(Object time) { public long getSqlLongTime(Object time) {
// logEntity.getTime()形式 "2024-02-04T14:03:45.114" // logEntity.getTime()形式 "2024-02-04T14:03:45.114"
// startTime()形式 "2024-02-04 14:03" // startTime()形式 "2024-02-04 14:03"
Date date; Date date;
@ -529,4 +541,342 @@ public class LogService {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public JSONObject setLogConfig(Integer threshold, Integer backupCycle, String backupStartTime) {
List<LogBackup> logBackupList = logBackupRepository.all().fetch();
if (logBackupList.size() == 0) {
LogBackup logBackup = new LogBackup();
logBackup.setBackupCycle(backupCycle);
logBackup.setThreshold(threshold);
logBackup.setBackupStartTime(backupStartTime);
saveBackup(logBackup);
} else {
LogBackup logBackupSelectResult = logBackupRepository.find(logBackupList.get(0).getId());
logBackupSelectResult.setBackupCycle(backupCycle);
logBackupSelectResult.setThreshold(threshold);
logBackupSelectResult.setBackupStartTime(backupStartTime);
saveBackup(logBackupSelectResult);
}
LogConfig logConfig = new LogConfig();
logConfig.setThreshold(threshold);
logConfig.setBackupCycle(backupCycle);
logConfig.setStatus(LogStatus.waitBackup);
// 起始备份时间为手动输入的备份开始时间,结束时间需要通过备份周期计算
logConfig.setLogStartTime(backupStartTime);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
Date date = null;
String logEndTime = null;
try {
date = simpleDateFormat.parse(backupStartTime);
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, backupCycle);
Long longTime = calendar.getTime().getTime();
// 将日志结束时间转化为"yyyy-MM-dd HH:mm:ss"格式
logEndTime = simpleDateFormat.format(longTime);
} catch (ParseException e) {
throw new RuntimeException(e);
}
logConfig.setLogEndTime(logEndTime);
List<LogConfig> list = logConfigRepository.all().fetch();
if (list.size() != 0) {
// 说明已经有记录了,要先将原来的主配置找到改为历史配置
for (LogConfig config : list) {
if ("Current".equals(config.getRecordType())) {
LogConfig logConfig1 = logConfigRepository.find(config.getId());
logConfig1.setRecordType("History");
saveConfig(logConfig1);
break;
}
}
}
logConfig.setRecordType("Current");
saveConfig(logConfig);
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 200);
jsonObject.put("msg", "保存成功");
return jsonObject;
}
@Transactional
public LogConfig saveConfig(LogConfig entity) {
return JPA.save(entity);
}
@Transactional
public LogBackup saveBackup(LogBackup entity) {
return JPA.save(entity);
}
public void deleteSystemManageUserBackup(ActionRequest request, ActionResponse response) {
Long id = (Long) request.getContext().get("id");
LogConfig logConfig = logConfigRepository.find(id);
if (null == logConfig) {
response.setError("未查询到此条记录");
return;
}
String filePath = logConfig.getFilePath();
File file = new File(filePath + "systemManageUser.zip");
if (file.delete()) {
logConfig.setStatus(LogStatus.alreadyDelete);
response.setNotify("日志备份文件删除成功!");
saveConfig(logConfig);
response.setReload(true);
} else {
response.setError("日志备份文件删除失败!");
response.setReload(true);
}
}
public void secondBackup(ActionRequest request, ActionResponse response) {
Long id = (Long) request.getContext().get("id");
LogConfig logConfig = logConfigRepository.find(id);
if (null == logConfig) {
response.setError("未查询到此条记录");
response.setReload(true);
return;
}
if (!LogStatus.backupFail.getValue().equals(logConfig.getStatus().getValue())) {
response.setNotify("该条记录状态非备份失败");
response.setReload(true);
return;
}
String backupTime = null;
String filePath = null;
try {
filePath = backupLogExcelZip(selectRecord(logConfig));
} catch (IOException e) {
response.setError("重新备份失败!");
response.setReload(true);
throw new RuntimeException(e);
}
// 需要将状态改为已备份,并添加备份时间,计算备份大小,将当前配置改为历史配置
logConfig.setStatus(LogStatus.alreadyBackup);
backupTime = getStringTime(System.currentTimeMillis());
logConfig.setBackupStartTime(backupTime);
logConfig.setBackupTime(backupTime);
// 计算文件大小,单位以M表示,精确小数点后两位
Map<String, String> fileSizeMap = getSize(filePath);
logConfig.setThresholdSystemManageUserSize(fileSizeMap.get("16"));
logConfig.setThresholdSecurityUserSize("1");
logConfig.setThresholdSecurityAuditUserSize("2");
logConfig.setThresholdServiceUserSize("3");
logConfig.setFilePath(filePath);
if (logConfig.getRecordType().equals("Current")) {
logConfig.setRecordType("History");
// 如果是当前配置,要添加一条新的记录,将记录改为待配置,因为当前是主配置,历史配置的话不需要,备份一次即可
LogConfig newRecord = new LogConfig();
newRecord.setRecordType("Current");
// 日志开始时间就是上次备份时间,根据备份周期计算日志结束时间
newRecord.setLogStartTime(backupTime);
newRecord.setLogEndTime(getEndTime(backupTime, logConfig.getBackupCycle()));
newRecord.setStatus(LogStatus.waitBackup);
logConfigRepository.save(newRecord);
}
saveConfig(logConfig);
response.setReload(true);
}
public Map<String, List<LogEntity>> selectRecord(LogConfig logConfig) {
Map<String, List<LogEntity>> map = new HashMap<>();
List<LogEntity> serviceUserSelectResult = new ArrayList<>();
List<LogEntity> securityUserSelectResult = new ArrayList<>();
List<LogEntity> securityAuditUserSelectResult = new ArrayList<>();
List<LogEntity> systemManageUserResult = new ArrayList<>();
List<LogEntity> logEntityList = logEntityRepository.all().fetch();
for (LogEntity logEntity : logEntityList) {
if (getSqlLongTime(logEntity.getTime()) > getLongTime(logConfig.getLogStartTime())
&& getSqlLongTime(logEntity.getTime()) < getLongTime(logConfig.getLogEndTime())) {
// 如果当条记录时间大于开始时间小于结束时间,则判断角色
if (logEntity.getUserAuth().getValue().equals("16")) {
// 系统管理员
systemManageUserResult.add(logEntity);
}
if (logEntity.getUserAuth().getValue().equals("1")) {
// 安全保密员
securityUserSelectResult.add(logEntity);
}
if (logEntity.getUserAuth().getValue().equals("2")) {
// 安全审计员
securityAuditUserSelectResult.add(logEntity);
}
if (logEntity.getUserAuth().getValue().equals("3")) {
// 业务用户
serviceUserSelectResult.add(logEntity);
}
map.put("systemManageUser", systemManageUserResult);
map.put("securityUser", securityUserSelectResult);
map.put("securityAuditUser", securityAuditUserSelectResult);
map.put("serviceUser", serviceUserSelectResult);
}
}
return map;
}
// 备份日志,导出为Excel类型
public String backupLogExcelZip(Map<String, List<LogEntity>> selectResult) throws IOException {
String tempFilePath = null;
Set<Map.Entry<String, List<LogEntity>>> entries = selectResult.entrySet();
// 根据时间设定输出文件名
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");
String date = simpleDateFormat1.format(new Date(System.currentTimeMillis()));
for (Map.Entry<String, List<LogEntity>> entry : entries) {
// 创建一个工作簿,也就是Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 创建一个工作表
HSSFSheet sheet = wb.createSheet();
// 第一行,标题
HSSFRow row0 = sheet.createRow(0);
HSSFCell cell0 = row0.createCell(0);
cell0.setCellValue("操作日志表");
// 第二行,表头
HSSFRow row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("id");
row1.createCell(1).setCellValue("address");
row1.createCell(2).setCellValue("content");
row1.createCell(3).setCellValue("hashValue");
row1.createCell(4).setCellValue("levelDesc");
row1.createCell(5).setCellValue("levelNum");
row1.createCell(6).setCellValue("module");
row1.createCell(7).setCellValue("objectName");
row1.createCell(8).setCellValue("opType");
row1.createCell(9).setCellValue("result");
row1.createCell(10).setCellValue("resultDesc");
row1.createCell(11).setCellValue("status");
row1.createCell(12).setCellValue("time");
row1.createCell(13).setCellValue("userAuth");
row1.createCell(14).setCellValue("userAuthDesc");
row1.createCell(15).setCellValue("userId");
// 向表中写入数据
int rowInt = 2;
for (LogEntity logEntity : entry.getValue()) {
HSSFRow row = sheet.createRow(rowInt++);
int index = 0;
row.createCell(index++).setCellValue(logEntity.getId());
row.createCell(index++).setCellValue(logEntity.getAddress());
row.createCell(index++).setCellValue(logEntity.getContent());
row.createCell(index++).setCellValue(logEntity.getHashValue());
row.createCell(index++).setCellValue(logEntity.getLevelDesc());
row.createCell(index++).setCellValue(logEntity.getLevelNum().getValue());
row.createCell(index++).setCellValue(logEntity.getModule());
row.createCell(index++).setCellValue(logEntity.getObjectName());
row.createCell(index++).setCellValue(logEntity.getOpType().getValue());
row.createCell(index++).setCellValue(logEntity.getResult());
row.createCell(index++).setCellValue(logEntity.getResultDesc());
row.createCell(index++).setCellValue(logEntity.getStatus());
row.createCell(index++).setCellValue(logEntity.getTime().toString());
row.createCell(index++).setCellValue(logEntity.getUserAuth().getValue());
row.createCell(index++).setCellValue(logEntity.getUserAuthDesc());
row.createCell(index++).setCellValue(logEntity.getUserId());
}
FileOutputStream fos = null;
try {
// 输出文件,创建字节输出流
File tempFile = new File(File.createTempFile("log", ".xls").getPath());
fos = new FileOutputStream(tempFile);
wb.write(fos);
fos.flush();
// 获取临时文件目录
String tempDir = System.getProperty("java.io.tmpdir");
tempFilePath = tempDir + "\\" + date;
String realFilePath = tempDir + "\\" + date + entry.getKey() + ".zip";
// 在临时文件目录中创建一个随机名称的zip文件
File file = new File(realFilePath);
ZipFile zipFile = new ZipFile(file);
ZipParameters parameters = new ZipParameters(); // 设置zip包的一些参数集合
// parameters.setEncryptFiles(true); // 是否设置密码(此处设置为:是)
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式(默认值)
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 普通级别(参数很多)
// parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密级别
// 向zip文件中根据设置的参数写入数据
zipFile.createZipFile(tempFile, parameters);
} catch (ZipException e) {
throw new RuntimeException(e);
} finally {
fos.close();
}
}
return tempFilePath;
}
public String getStringTime(long time) {
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat1.format(time);
}
public String getEndTime(String time, Integer backupCycle) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
Date date = null;
String logEndTime = null;
try {
date = simpleDateFormat.parse(time);
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, backupCycle);
Long longTime = calendar.getTime().getTime();
// 将日志结束时间转化为"yyyy-MM-dd HH:mm:ss"格式
logEndTime = simpleDateFormat.format(longTime);
return logEndTime;
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public Map<String, String> getSize(String filePath) {
Map<String, String> map = new HashMap<>();
map.put("16", getFileSize(filePath + "systemManageUser.zip"));
map.put("1", getFileSize(filePath + "securityUser.zip"));
map.put("2", getFileSize(filePath + "securityAuditUser.zip"));
map.put("3", getFileSize(filePath + "serviceUser.zip"));
return map;
}
// 计算文件大小,单位为M,保留两位小数
public String getFileSize(String filePath) {
File file = new File(filePath);
long fileSize = file.length();
DecimalFormat df = new DecimalFormat("#.00");
// 将字节转换为兆字节(MB)并格式化输出
double fileSizeInKB = (double) fileSize / 1024;
if (fileSizeInKB > 1024) {
double fileSizeInMB = (double) fileSize / (1024 * 1024);
if (fileSizeInMB > 1024) {
double fileSizeInGB = (double) fileSize / (1024 * 1024 * 1024);
String formattedNumber = df.format(fileSizeInGB);
double result = Double.parseDouble(formattedNumber);
return result + "GB";
}
String formattedNumber = df.format(fileSizeInMB);
double result = Double.parseDouble(formattedNumber);
return result + "MB";
}
String formattedNumber = df.format(fileSizeInKB);
double result = Double.parseDouble(formattedNumber);
return result + "KB";
}
public JSONObject selectLogConfig() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", 200);
jsonObject.put("msg", "操作成功");
List<LogBackup> logBackupList = logBackupRepository.all().fetch();
if (logBackupList.size() == 0) {
jsonObject.put("data", null);
return jsonObject;
} else {
JSONObject selectResult = new JSONObject();
selectResult.put("threshold", logBackupList.get(0).getThreshold());
selectResult.put("backupCycle", logBackupList.get(0).getBackupCycle());
selectResult.put("backupStartTime", logBackupList.get(0).getBackupStartTime());
jsonObject.put("data", selectResult);
return jsonObject;
}
}
} }

23
src/main/resources/application.properties

@ -62,14 +62,14 @@ auth.logout.local=true
#db.default.password = hypaas #db.default.password = hypaas
# MySQL # MySQL
db.default.driver= db.default.driver=com.mysql.cj.jdbc.Driver
db.default.ddl=update db.default.ddl=update
db.default.url=jdbc:://?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true db.default.url=jdbc:mysql://139.9.112.197:3308/portal-gateway?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&nullCatalogMeansCurrent=true
db.default.user= db.default.user=portal
db.default.password= db.default.password=Sun@qc2023
system.operate.log.switch=true system.operate.log.switch=false
event.center.url=http://123.57.213.228:1117/prod-api/event/send_event_info event.center.url=http://123.57.213.228:1117/prod-api/event/send_event_info
#db.company1.name=Company 1 #db.company1.name=Company 1
@ -239,7 +239,7 @@ ldap.user.filter=(uid={0})
# ~~~~~ # ~~~~~
# quartz job scheduler # quartz job scheduler
# Specify whether to enable quartz scheduler # Specify whether to enable quartz scheduler
quartz.enable=false quartz.enable=true
# total number of threads in quartz thread pool # total number of threads in quartz thread pool
# the number of jobs that can run simultaneously # the number of jobs that can run simultaneously
quartz.threadCount=3 quartz.threadCount=3
@ -321,14 +321,9 @@ logging.level.com.hypaas=INFO
# Log connection pooling # Log connection pooling
#logging.level.com.zaxxer.hikari = INFO #logging.level.com.zaxxer.hikari = INFO
auth.jwt.basic.auth.enabled=true #auth.jwt.basic.auth.enabled=true
application.microcontext=/lowcode-portal-logmanage/ #application.microcontext=/lowcode-portal-logmanage/
application.gatewaycontext=/lowcode-portal-logmanage/ #application.gatewaycontext=/lowcode-portal-logmanage/

Loading…
Cancel
Save