网站首页/ 信息中心/ 档案百科/

档案管理系统审计追踪功能从零搭建实操落地指南

发布时间:2026年07月06日 03:25:22 浏览量:0

一、前期环境准备

使用SpringBoot框架快速实现,需提前安装:JDK1.8+、MySQL8.0+、Maven3.6+,所有工具均为通用开源工具,无需额外付费产品。

二、核心代码配置(直接复制使用)

2.1 项目依赖配置(pom.xml)

将以下内容替换到项目pom.xml的dependencies节点中,版本为稳定兼容版:

```xml org.springframework.boot spring-boot-starter-web 2.7.12 mysql mysql-connector-java 8.0.33 com.baomidou mybatis-plus-boot-starter 3.5.3.1 org.projectlombok lombok true org.springframework.boot spring-boot-starter-security 2.7.12 ```

2.2 审计日志表创建(SQL)

登录MySQL执行以下建表语句,用于存储所有档案操作记录:

```sql CREATE TABLE `audit_log` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID', `operator` varchar(64) NOT NULL COMMENT '操作人', `operate_time` datetime NOT NULL COMMENT '操作时间', `operate_type` varchar(16) NOT NULL COMMENT '操作类型:新增/修改/删除', `archive_id` bigint DEFAULT NULL COMMENT '关联档案ID', `old_data` text COMMENT '修改前数据', `new_data` text COMMENT '修改后数据', `remark` varchar(256) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='审计日志表'; ```

重点:执行前需先创建archive_db数据库,命令:CREATE DATABASE archive_db DEFAULT CHARSET=utf8mb4;

2.3 核心功能代码实现

2.3.1 审计日志实体类

```java import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import java.time.LocalDateTime; @Data @TableName("audit_log") public class AuditLog { private Long id; private String operator; private LocalDateTime operateTime; private String operateType; private Long archiveId; private String oldData; private String newData; private String remark; } ```

2.3.2 审计日志Mapper接口

档案管理系统审计追踪功能从零搭建实操落地指南

继承MyBatis Plus基础接口,无需额外实现:

```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; @Mapper public interface AuditLogMapper extends BaseMapper { } ```

2.3.3 审计拦截AOP切面

用于拦截档案操作方法,自动记录日志:

```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; @Aspect @Component public class AuditLogAspect { @Autowired private AuditLogMapper auditLogMapper; @Around("execution( com.example.archive.service.ArchiveService.(..))") public Object recordAuditLog(ProceedingJoinPoint point) throws Throwable { // 获取当前操作人 Authentication auth = SecurityContextHolder.getContext().getAuthentication(); String operator = auth == null ? "匿名用户" : auth.getName(); // 判断操作类型 String methodName = point.getSignature().getName(); String operateType; if (methodName.startsWith("save")) operateType = "新增"; else if (methodName.startsWith("update")) operateType = "修改"; else if (methodName.startsWith("delete")) operateType = "删除"; else operateType = "其他"; // 获取操作参数(转JSON) Object[] args = point.getArgs(); String oldData = ""; String newData = ""; Long archiveId = null; if (args.length > 0) { Object param = args[0]; ObjectMapper objectMapper = new ObjectMapper(); if (operateType.equals("新增") || operateType.equals("修改")) { newData = objectMapper.writeValueAsString(param); if (operateType.equals("修改")) archiveId = ((com.example.archive.entity.Archive) param).getId(); } else if (operateType.equals("删除")) { archiveId = (Long) args[0]; oldData = "删除ID:" + archiveId; } } // 执行原档案操作方法 Object result = point.proceed(); // 插入审计日志 AuditLog auditLog = new AuditLog(); auditLog.setOperator(operator); auditLog.setOperateTime(java.time.LocalDateTime.now()); auditLog.setOperateType(operateType); auditLog.setArchiveId(archiveId); auditLog.setOldData(oldData); auditLog.setNewData(newData); auditLog.setRemark("档案操作"); auditLogMapper.insert(auditLog); return result; } } ```

重点:将代码中的包路径com.example.archive替换为你项目的实际包路径,否则拦截不到档案操作方法

2.4 配置文件(application.yml)

配置数据库连接和测试账号,方便快速验证:

```yaml spring: datasource: url: jdbc:mysql://localhost:3306/archive_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root password: 123456 替换为你的MySQL root密码 driver-class-name: com.mysql.cj.jdbc.Driver security: user: name: admin password: admin123 测试登录账号密码 ```

三、功能验证步骤

  1. 启动项目:执行SpringBoot主类,启动服务(默认端口8080)
  2. 登录测试:用Postman或浏览器访问http://localhost:8080/login,输入账号admin,密码admin123
  3. 执行档案操作:发送POST请求到http://localhost:8080/archive/save,Body选择raw→JSON,内容为{"name":"测试档案","code":"TEST001"}
  4. 验证审计日志:执行MySQL查询:SELECT FROM audit_log;,可看到一条操作类型为「新增」的记录,操作人显示为admin
2026年档案继续教育报名流程、时间及要求全解析
2026年档案继续教育报名流程、时间及要求全解析
2026年档案继续教育报名需满足资质条件、在规定时间内通过官方指定渠道完成申请,本回答将从报名前提、操作流程、注意事项及学分认证四个维度详细拆解实操方案,帮助报名者高效完成注册。
2026年07月06日 03:25:22
2026年企业档案软件文件加密怎么做?最安全的方法有哪些?
2026年企业档案软件文件加密怎么做?最安全的方法有哪些?
实现档案软件文件加密主要依托透明加密技术和精细化的权限管控体系,确保电子档案在存储、使用及流转全生命周期的安全性。2026年数据安全合规要求更加严格,采用符合国家标准的加密手段已成为企业刚需。本回答将...
2026年07月06日 03:25:22
微信咨询
电话联系
QQ客服
微信咨询一对一服务
服务热线: 028-8744 4417
QQ客服: 2305721818