1. 添加日志审计功能

jinan_dev
xueqingkun 5 months ago
parent f8340cfcf3
commit 2fdbc3f2f0

@ -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<String, Object> 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);
}

@ -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<String> USER = new ThreadLocal<>();
public static final ThreadLocal<AuditLogDTO> AUDIT_LOG = new ThreadLocal<>();
}

@ -9,7 +9,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
*
*
* @TableName attribute_gwzs
*/
@TableName(value ="attribute_gwzs")

@ -9,7 +9,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
*
*
* @TableName attribute_xmht
*/
@TableName(value ="attribute_xmht")

@ -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;
}

@ -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);
}
}

@ -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<AuditLog> {
}

@ -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<AuditLog> {
void saveAuditLog(AuditLogDTO auditLogDTO);
}

@ -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<AuditLogMapper, AuditLog>
implements AuditLogService{
@Override
public void saveAuditLog(AuditLogDTO auditLogDTO) {
if (null == auditLogDTO){
return;
}
AuditLog auditLog = auditLogDTO.toAuditLog();
this.save(auditLog);
}
}

@ -837,7 +837,7 @@ public class CaseEvidenceServiceImpl extends ServiceImpl<CaseEvidenceMapper, Cas
// 同时更新证据属性值
String categoryId = rootDirectory.findDirectory(CollUtil.getFirst(value).getDirectoryId()).getCategoryId();
Map<String, List<NotePromptExtractAttributesDto>> 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());

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.supervision.police.mapper.AuditLogMapper">
<resultMap id="BaseResultMap" type="com.supervision.police.domain.AuditLog">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="recordType" column="record_type" jdbcType="VARCHAR"/>
<result property="userId" column="user_id" jdbcType="VARCHAR"/>
<result property="userName" column="user_name" jdbcType="VARCHAR"/>
<result property="ip" column="ip" jdbcType="VARCHAR"/>
<result property="method" column="method" jdbcType="VARCHAR"/>
<result property="url" column="url" jdbcType="VARCHAR"/>
<result property="requestParams" column="request_params" jdbcType="VARCHAR"/>
<result property="costTime" column="cost_time" jdbcType="INTEGER"/>
<result property="response" column="response" jdbcType="VARCHAR"/>
<result property="exceptionDesc" column="exception_desc" jdbcType="VARCHAR"/>
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="createUserId" column="create_user_id" jdbcType="VARCHAR"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="updateUserId" column="update_user_id" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
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
</sql>
</mapper>
Loading…
Cancel
Save