- IT频道
- 时间:2025-09-14 19:35
- 阅读:124
一、需求分析
1. 日志记录范围:
- 用户登录/登出操作
- 关键业务操作(如订单创建、修改、取消)
- 敏感数据访问(如价格修改、库存调整)
- 系统配置变更
- 异常操作尝试(如权限不足操作)
2. 日志内容要求:
- 操作时间戳(精确到毫秒)
- 操作用户ID及角色
- 操作类型(CRUD)
- 操作对象(如订单ID、商品ID)
- 操作前后的数据快照(可选)
- 操作结果(成功/失败及原因)
- 客户端IP地址
二、技术方案设计
1. 日志存储结构
```json
{
"log_id": "唯一标识符",
"timestamp": "2023-11-15T14:30:45.123Z",
"user_id": "U12345",
"username": "张三",
"role": "采购员",
"operation_type": "UPDATE",
"module": "订单管理",
"target_id": "ORD20231115001",
"description": "修改订单收货地址",
"old_value": "原地址:北京市朝阳区...",
"new_value": "新地址:上海市浦东新区...",
"result": "SUCCESS",
"client_ip": "192.168.1.100",
"user_agent": "Mozilla/5.0..."
}
```
2. 技术实现方案
方案一:AOP切面实现(推荐)
```java
@Aspect
@Component
public class OperationLogAspect {
@Autowired
private OperationLogService logService;
@Pointcut("@annotation(com.kuailv.annotation.OperationLog)")
public void logPointCut() {}
@Around("logPointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取方法签名
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
// 获取注解信息
OperationLog operationLog = method.getAnnotation(OperationLog.class);
// 获取用户信息(从ThreadLocal或SecurityContext)
UserDTO user = UserContext.getCurrentUser();
long startTime = System.currentTimeMillis();
Object result = null;
try {
result = joinPoint.proceed();
// 记录成功日志
saveLog(joinPoint, operationLog, user, "SUCCESS", null);
return result;
} catch (Exception e) {
// 记录失败日志
saveLog(joinPoint, operationLog, user, "FAILED", e.getMessage());
throw e;
} finally {
// 计算耗时
long costTime = System.currentTimeMillis() - startTime;
// 可选:更新日志中的耗时信息
}
}
private void saveLog(ProceedingJoinPoint joinPoint, OperationLog operationLog,
UserDTO user, String result, String errorMsg) {
// 实现日志保存逻辑
OperationLogDTO logDTO = new OperationLogDTO();
// 填充日志信息...
logService.save(logDTO);
}
}
```
方案二:手动记录(适用于复杂场景)
```java
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OperationLogService logService;
@Override
public Order updateOrder(OrderUpdateDTO updateDTO, String operatorId) {
// 1. 记录操作前数据
Order originalOrder = orderRepository.findById(updateDTO.getOrderId());
// 2. 执行业务逻辑
Order updatedOrder = // 更新逻辑...
// 3. 记录操作日志
OperationLogDTO log = new OperationLogDTO();
log.setOperationType("UPDATE");
log.setModule("订单管理");
log.setTargetId(updateDTO.getOrderId());
log.setDescription("修改订单信息");
log.setOldValue(JSON.toJSONString(originalOrder));
log.setNewValue(JSON.toJSONString(updatedOrder));
log.setOperatorId(operatorId);
log.setResult("SUCCESS");
logService.save(log);
return updatedOrder;
}
}
```
3. 日志存储方案
选项1:数据库存储(MySQL)
```sql
CREATE TABLE operation_log (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
log_id VARCHAR(64) NOT NULL COMMENT 日志唯一ID,
operation_time DATETIME(3) NOT NULL COMMENT 操作时间,
user_id VARCHAR(64) NOT NULL COMMENT 用户ID,
username VARCHAR(100) COMMENT 用户名,
role VARCHAR(50) COMMENT 角色,
operation_type VARCHAR(20) NOT NULL COMMENT 操作类型,
module VARCHAR(50) NOT NULL COMMENT 功能模块,
target_id VARCHAR(64) COMMENT 操作对象ID,
description VARCHAR(500) COMMENT 操作描述,
old_value TEXT COMMENT 操作前数据,
new_value TEXT COMMENT 操作后数据,
result VARCHAR(20) NOT NULL COMMENT 操作结果,
client_ip VARCHAR(50) COMMENT 客户端IP,
user_agent VARCHAR(500) COMMENT 用户代理,
create_time DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3) COMMENT 创建时间
) ENGINE=InnoDB COMMENT=用户操作日志表;
```
选项2:ELK Stack(适合大规模日志)
- Elasticsearch:存储和索引日志
- Logstash:日志收集和转换
- Kibana:日志可视化和查询
4. 日志查询接口
```java
@RestController
@RequestMapping("/api/logs")
public class OperationLogController {
@Autowired
private OperationLogService logService;
@GetMapping
public PageResult queryLogs(
@RequestParam(required = false) String userId,
@RequestParam(required = false) String module,
@RequestParam(required = false) String operationType,
@RequestParam(required = false) Date startTime,
@RequestParam(required = false) Date endTime,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "20") Integer pageSize) {
return logService.queryLogs(userId, module, operationType,
startTime, endTime, pageNum, pageSize);
}
@GetMapping("/export")
public void exportLogs(HttpServletResponse response,
@RequestParam(required = false) String userId,
@RequestParam(required = false) String module,
@RequestParam(required = false) String operationType,
@RequestParam(required = false) Date startTime,
@RequestParam(required = false) Date endTime) throws IOException {
List logs = logService.exportLogs(userId, module,
operationType, startTime, endTime);
// 导出为Excel或CSV
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=operation_logs.xlsx");
// 使用EasyExcel或Apache POI导出...
}
}
```
三、开发实现步骤
1. 定义日志注解:
```java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLog {
String module() default "";
String description() default "";
boolean saveParams() default true;
boolean saveResult() default false;
}
```
2. 实现日志服务:
```java
@Service
public class OperationLogServiceImpl implements OperationLogService {
@Autowired
private OperationLogMapper logMapper;
@Override
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void save(OperationLogDTO logDTO) {
// 转换DTO为Entity
OperationLogEntity entity = convertToEntity(logDTO);
logMapper.insert(entity);
}
@Override
public PageResult queryLogs(String userId, String module,
String operationType, Date startTime,
Date endTime, Integer pageNum,
Integer pageSize) {
// 实现分页查询逻辑
}
}
```
3. 前端日志查看页面:
- 实现日志列表展示
- 支持按用户、模块、时间范围等条件筛选
- 支持导出Excel功能
四、安全与性能考虑
1. 安全措施:
- 日志中敏感信息脱敏(如手机号、地址等)
- 日志查询权限控制(只有管理员可查看)
- 防止日志注入攻击
2. 性能优化:
- 异步记录日志(使用@Async)
- 批量插入日志
- 对高频操作采用采样记录
- 设置日志保留周期(如只保留90天日志)
3. 监控告警:
- 监控异常操作频率
- 对敏感操作(如删除、权限修改)实时告警
五、测试用例
1. 正常流程测试:
- 用户正常操作是否记录日志
- 日志内容是否完整准确
2. 异常流程测试:
- 操作失败时是否记录失败日志
- 并发操作时的日志完整性
3. 性能测试:
- 高并发场景下的日志记录性能
- 日志查询响应时间
4. 安全测试:
- 普通用户是否能查看日志
- 日志中是否包含敏感信息
六、部署与维护
1. 日志轮转:
- 配置日志文件轮转策略
- 定期归档旧日志
2. 日志清理:
- 设置自动清理策略(如按时间或大小)
3. 监控:
- 监控日志系统健康状态
- 设置日志写入失败告警
七、后续优化方向
1. 增加日志分析功能(如操作热力图)
2. 实现日志行为模式识别(异常行为检测)
3. 集成SIEM系统进行安全分析
4. 支持多维度日志统计分析
通过以上方案,快驴生鲜系统可以建立完善的用户操作日志体系,既满足合规审计要求,又能为系统运维和安全分析提供有力支持。
全部评论(0)
推荐阅读
![]()
- IT频道
- 时间:2026-05-28 04:25
- 阅读:1
![]()
- IT频道
- 时间:2026-05-28 04:20
- 阅读:1
![]()
- IT频道
- 时间:2026-05-28 04:15
- 阅读:1
![]()
- IT频道
- 时间:2026-05-28 04:10
- 阅读:1
![]()
- IT频道
- 时间:2026-05-28 04:05
- 阅读:1