From 2fdbc3f2f0cf07d96ab351a29f8b1b122a6f313e Mon Sep 17 00:00:00 2001 From: xueqingkun Date: Tue, 10 Dec 2024 14:26:05 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=AE=A1=E8=AE=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../supervision/config/RequestLogConfig.java | 76 +++++++++- .../com/supervision/config/ThreadCache.java | 4 + .../police/domain/AttributeGwzs.java | 2 +- .../police/domain/AttributeXmht.java | 2 +- .../supervision/police/domain/AuditLog.java | 96 ++++++++++++ .../supervision/police/dto/AuditLogDTO.java | 137 ++++++++++++++++++ .../police/mapper/AuditLogMapper.java | 18 +++ .../police/service/AuditLogService.java | 16 ++ .../service/impl/AuditLogServiceImpl.java | 32 ++++ .../service/impl/CaseEvidenceServiceImpl.java | 2 +- src/main/resources/mapper/AuditLogMapper.xml | 32 ++++ 11 files changed, 406 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/supervision/police/domain/AuditLog.java create mode 100644 src/main/java/com/supervision/police/dto/AuditLogDTO.java create mode 100644 src/main/java/com/supervision/police/mapper/AuditLogMapper.java create mode 100644 src/main/java/com/supervision/police/service/AuditLogService.java create mode 100644 src/main/java/com/supervision/police/service/impl/AuditLogServiceImpl.java create mode 100644 src/main/resources/mapper/AuditLogMapper.xml diff --git a/src/main/java/com/supervision/config/RequestLogConfig.java b/src/main/java/com/supervision/config/RequestLogConfig.java index c62a3d2..ce5267c 100644 --- a/src/main/java/com/supervision/config/RequestLogConfig.java +++ b/src/main/java/com/supervision/config/RequestLogConfig.java @@ -2,6 +2,8 @@ package com.supervision.config; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; +import com.supervision.police.dto.AuditLogDTO; +import com.supervision.police.service.AuditLogService; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.http.HttpServletRequest; @@ -9,16 +11,17 @@ import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.AfterThrowing; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import org.springframework.util.ObjectUtils; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.multipart.MultipartFile; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; @@ -34,22 +37,49 @@ import java.util.Map; public class RequestLogConfig { + @Autowired + private AuditLogService auditLogService; + @SuppressWarnings("all") @Around("within(com..*..controller..*) && @within(org.springframework.web.bind.annotation.RestController)") public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { long start = System.currentTimeMillis(); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); + + AuditLogDTO auditLogDTO = new AuditLogDTO(request); + Map requestParams = getRequestParamsByProceedingJoinPoint(proceedingJoinPoint); + auditLogDTO.setRequestParams(JSONUtil.toJsonStr(requestParams)); + ThreadCache.AUDIT_LOG.set(auditLogDTO);// 把数据缓存到treadLocal中 + // 执行目标方法 Object result = proceedingJoinPoint.proceed(); + + + // 目标方法执行后 RequestInfo requestInfo = new RequestInfo(); requestInfo.setUrl(request.getRequestURL().toString()); requestInfo.setHttpMethod(request.getMethod()); requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(), proceedingJoinPoint.getSignature().getName())); - requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint)); - requestInfo.setResult(proceedResult(result)); + requestInfo.setRequestParams(requestParams); + Object response = proceedResult(result); + requestInfo.setResult(response); requestInfo.setTimeCost(System.currentTimeMillis() - start); log.info("Request Info: {}", JSONUtil.toJsonStr(requestInfo)); + + + try { + auditLogDTO.setRecordType("0"); + if (response instanceof String){ + auditLogDTO.setResponse((String) response); + } + Thread.sleep(1000); + auditLogDTO.setEndTime(LocalDateTime.now()); + auditLogService.saveAuditLog(auditLogDTO); + } catch (Exception e) { + log.error("保存审计信息出错,保存内容:{}",JSONUtil.toJsonStr(auditLogDTO), e); + } + return result; } @@ -64,6 +94,9 @@ public class RequestLogConfig { value = value.substring(0, 1024) + "......" + value.substring(value.length() - 1024); return value; } + if (StrUtil.isNotEmpty(value)){ + return value; + } return result; } @@ -81,8 +114,35 @@ public class RequestLogConfig { requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint)); requestErrorInfo.setException(e); log.info("Error Request Info : {}", JSONUtil.toJsonStr(requestErrorInfo)); + + AuditLogDTO auditLogDTO = ThreadCache.AUDIT_LOG.get(); + if (null != auditLogDTO){ + try { + auditLogDTO.setEndTime(LocalDateTime.now()); + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + auditLogDTO.setExceptionDesc(sw.toString()); + auditLogDTO.setRecordType("1"); + auditLogService.saveAuditLog(auditLogDTO); + } catch (Exception ex) { + log.warn("保存审计信息出错,保存内容:{}",JSONUtil.toJsonStr(auditLogDTO), ex); + } + } + } + + @After(value = "@within(org.springframework.web.bind.annotation.RestController)") + public void doAfter(JoinPoint joinPoint) { + + // 清理线程变量 + ThreadCache.AUDIT_LOG.remove(); + + } + + + + /** * 获取入参 * @@ -125,13 +185,13 @@ public class RequestLogConfig { MultipartFile file = (MultipartFile) value; //获取文件名 value = file.getOriginalFilename(); - } else if (value instanceof String) { + } /*else if (value instanceof String) { // 如果参数很长,就对参数进行修剪 String temp = (String) value; if (!ObjectUtils.isEmpty(temp) && temp.length() > 1024) { value = temp.substring(0, 10) + "......" + temp.substring(temp.length() - 10); } - } + }*/ requestParams.put(paramNames[i], value); } diff --git a/src/main/java/com/supervision/config/ThreadCache.java b/src/main/java/com/supervision/config/ThreadCache.java index 4b98e76..833c4b1 100644 --- a/src/main/java/com/supervision/config/ThreadCache.java +++ b/src/main/java/com/supervision/config/ThreadCache.java @@ -10,6 +10,7 @@ */ package com.supervision.config; +import com.supervision.police.dto.AuditLogDTO; /** * <功能详细描述> @@ -27,4 +28,7 @@ public class ThreadCache { public static final ThreadLocal USER = new ThreadLocal<>(); + public static final ThreadLocal AUDIT_LOG = new ThreadLocal<>(); + + } diff --git a/src/main/java/com/supervision/police/domain/AttributeGwzs.java b/src/main/java/com/supervision/police/domain/AttributeGwzs.java index 158862e..a4be94d 100644 --- a/src/main/java/com/supervision/police/domain/AttributeGwzs.java +++ b/src/main/java/com/supervision/police/domain/AttributeGwzs.java @@ -9,7 +9,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; /** - * + * 公文证书属性 * @TableName attribute_gwzs */ @TableName(value ="attribute_gwzs") diff --git a/src/main/java/com/supervision/police/domain/AttributeXmht.java b/src/main/java/com/supervision/police/domain/AttributeXmht.java index 2cd4d09..f05194a 100644 --- a/src/main/java/com/supervision/police/domain/AttributeXmht.java +++ b/src/main/java/com/supervision/police/domain/AttributeXmht.java @@ -9,7 +9,7 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; /** - * + * 项目合同属性 * @TableName attribute_xmht */ @TableName(value ="attribute_xmht") diff --git a/src/main/java/com/supervision/police/domain/AuditLog.java b/src/main/java/com/supervision/police/domain/AuditLog.java new file mode 100644 index 0000000..0353faa --- /dev/null +++ b/src/main/java/com/supervision/police/domain/AuditLog.java @@ -0,0 +1,96 @@ +package com.supervision.police.domain; + +import com.baomidou.mybatisplus.annotation.*; + +import java.io.Serializable; +import java.time.LocalDateTime; +import lombok.Data; + +/** + * 日志审计表 + * @TableName audit_log + */ +@TableName(value ="audit_log") +@Data +public class AuditLog implements Serializable { + /** + * 主键id + */ + @TableId + private String id; + + /** + * 记录类型:0-正常操作记录;1-异常记录 + */ + private String recordType; + + /** + * 用户id + */ + private String userId; + + /** + * 用户名 + */ + private String userName; + + /** + * 操作人ip地址 + */ + private String ip; + + /** + * 请求类型 GET POST PUT DELETE + */ + private String method; + + /** + * url地址 + */ + private String url; + + /** + * 请求参数 + */ + private String requestParams; + + /** + * 操作耗时 单位ms + */ + private Long costTime; + + /** + * 响应结果 + */ + private String response; + + /** + * 异常描述 + */ + private String exceptionDesc; + + /** + * 创建时间 + */ + @TableField(fill = FieldFill.INSERT) + private LocalDateTime createTime; + + /** + * 创建人 + */ + private String createUserId; + + /** + * 更新时间 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + private LocalDateTime updateTime; + + /** + * 更新用户id + */ + private String updateUserId; + + @TableField(exist = false) + private static final long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/src/main/java/com/supervision/police/dto/AuditLogDTO.java b/src/main/java/com/supervision/police/dto/AuditLogDTO.java new file mode 100644 index 0000000..aef8932 --- /dev/null +++ b/src/main/java/com/supervision/police/dto/AuditLogDTO.java @@ -0,0 +1,137 @@ +package com.supervision.police.dto; +import com.supervision.police.domain.AuditLog; +import com.supervision.police.dto.user.UserInfoDTO; +import com.supervision.utils.UserUtil; +import jakarta.servlet.http.HttpServletRequest; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; + +import java.io.Serializable; +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +@Slf4j +@Data +public class AuditLogDTO implements Serializable { + /** + * 主键id + */ + private String id; + + /** + * 记录类型:0-正常操作记录;1-异常记录 + */ + private String recordType; + + /** + * 用户id + */ + private String userId; + + /** + * 用户名 + */ + private String userName; + + /** + * 操作人ip地址 + */ + private String ip; + + /** + * 请求类型 GET POST PUT DELETE + */ + private String method; + + /** + * url地址 + */ + private String url; + + /** + * 请求参数 + */ + private String requestParams; + + /** + * 响应结果 + */ + private String response; + + /** + * 异常描述 + */ + private String exceptionDesc; + + /** + * 创建时间 + */ + private LocalDateTime createTime; + + /** + * 创建人 + */ + private String createUserId; + + /** + * 更新时间 + */ + private LocalDateTime updateTime; + + /** + * 更新用户id + */ + private String updateUserId; + + private LocalDateTime startTime; + + private LocalDateTime endTime; + + + public AuditLogDTO() { + } + + public AuditLogDTO(HttpServletRequest request) { + + try { + UserInfoDTO user = UserUtil.getUser(); + this.userName = user.getUserName(); + this.userId = user.getId(); + } catch (Exception e) { + log.warn("未获取到用户信息", e); + } + this.url = request.getRequestURI(); + this.method = request.getMethod(); + this.ip = request.getRemoteAddr(); + this.startTime = LocalDateTime.now(); + } + + public AuditLog toAuditLog(){ + AuditLog auditLog = new AuditLog(); + auditLog.setId(id); + auditLog.setRecordType(recordType); + auditLog.setUserId(userId); + auditLog.setUserName(userName); + auditLog.setIp(ip); + auditLog.setMethod(method); + auditLog.setUrl(url); + auditLog.setRequestParams(requestParams); + auditLog.setCostTime(this.evaluateCostTime()); + auditLog.setResponse(response); + auditLog.setExceptionDesc(exceptionDesc); + auditLog.setCreateTime(createTime); + auditLog.setCreateUserId(createUserId); + auditLog.setUpdateTime(updateTime); + auditLog.setUpdateUserId(updateUserId); + return auditLog; + } + + public long evaluateCostTime() { + if (null == this.startTime || null == this.endTime){ + return 0; + } + return ChronoUnit.MILLIS.between(this.startTime, this.endTime); + } + + +} diff --git a/src/main/java/com/supervision/police/mapper/AuditLogMapper.java b/src/main/java/com/supervision/police/mapper/AuditLogMapper.java new file mode 100644 index 0000000..1e03128 --- /dev/null +++ b/src/main/java/com/supervision/police/mapper/AuditLogMapper.java @@ -0,0 +1,18 @@ +package com.supervision.police.mapper; + +import com.supervision.police.domain.AuditLog; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; + +/** +* @author Administrator +* @description 针对表【audit_log(日志审计表)】的数据库操作Mapper +* @createDate 2024-12-09 13:19:15 +* @Entity com.supervision.police.domain.AuditLog +*/ +public interface AuditLogMapper extends BaseMapper { + +} + + + + diff --git a/src/main/java/com/supervision/police/service/AuditLogService.java b/src/main/java/com/supervision/police/service/AuditLogService.java new file mode 100644 index 0000000..abcab4e --- /dev/null +++ b/src/main/java/com/supervision/police/service/AuditLogService.java @@ -0,0 +1,16 @@ +package com.supervision.police.service; + +import com.supervision.police.domain.AuditLog; +import com.baomidou.mybatisplus.extension.service.IService; +import com.supervision.police.dto.AuditLogDTO; + +/** +* @author Administrator +* @description 针对表【audit_log(日志审计表)】的数据库操作Service +* @createDate 2024-12-09 13:19:15 +*/ +public interface AuditLogService extends IService { + + + void saveAuditLog(AuditLogDTO auditLogDTO); +} diff --git a/src/main/java/com/supervision/police/service/impl/AuditLogServiceImpl.java b/src/main/java/com/supervision/police/service/impl/AuditLogServiceImpl.java new file mode 100644 index 0000000..a53859c --- /dev/null +++ b/src/main/java/com/supervision/police/service/impl/AuditLogServiceImpl.java @@ -0,0 +1,32 @@ +package com.supervision.police.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.supervision.police.domain.AuditLog; +import com.supervision.police.dto.AuditLogDTO; +import com.supervision.police.service.AuditLogService; +import com.supervision.police.mapper.AuditLogMapper; +import org.springframework.stereotype.Service; + +/** +* @author Administrator +* @description 针对表【audit_log(日志审计表)】的数据库操作Service实现 +* @createDate 2024-12-09 13:19:15 +*/ +@Service +public class AuditLogServiceImpl extends ServiceImpl + implements AuditLogService{ + + @Override + public void saveAuditLog(AuditLogDTO auditLogDTO) { + if (null == auditLogDTO){ + return; + } + + AuditLog auditLog = auditLogDTO.toAuditLog(); + this.save(auditLog); + } +} + + + + diff --git a/src/main/java/com/supervision/police/service/impl/CaseEvidenceServiceImpl.java b/src/main/java/com/supervision/police/service/impl/CaseEvidenceServiceImpl.java index 616881d..890d0cf 100644 --- a/src/main/java/com/supervision/police/service/impl/CaseEvidenceServiceImpl.java +++ b/src/main/java/com/supervision/police/service/impl/CaseEvidenceServiceImpl.java @@ -837,7 +837,7 @@ public class CaseEvidenceServiceImpl extends ServiceImpl> map = caseEvidencePropertyService.listEvidenceProperty(categoryId, Collections.singletonList(evidenceId)); - if (CollUtil.isEmpty(map.get(categoryId))) { + if (CollUtil.isEmpty(map.get(evidenceId))) { caseEvidencePropertyService.saveEvidenceProperty(evidenceId, categoryId, CollUtil.getFirst(value).getProperties()); } else { caseEvidencePropertyService.updateEvidenceProperty(evidenceId, categoryId, CollUtil.getFirst(value).getProperties()); diff --git a/src/main/resources/mapper/AuditLogMapper.xml b/src/main/resources/mapper/AuditLogMapper.xml new file mode 100644 index 0000000..585928d --- /dev/null +++ b/src/main/resources/mapper/AuditLogMapper.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + id,record_type,user_id, + user_name,ip,method, + url,request_params,cost_time, + response,exception_desc,create_time, + create_user_id,update_time,update_user_id + +