|
@@ -0,0 +1,175 @@
|
|
|
|
|
+package com.etotem.cfc.service;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import com.etotem.cfc.common.DataMaskUtil;
|
|
|
|
|
+import com.etotem.cfc.entity.DataExportTask;
|
|
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
|
|
+import com.etotem.cfc.entity.User;
|
|
|
|
|
+import com.etotem.cfc.mapper.DataExportTaskMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 数据导出(GDPR)服务(SC-P-72,能力键 {@code data.export.gdpr})。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p>「申请导出 → 生成加密包 → 下载」流程宿主:申请入口做频次限制(24h 窗口)+ 归属校验(越权拒),
|
|
|
|
|
+ * 打包阶段复用 {@link AccountCleanupService#exportUserData(Long, Long)} 汇集数据(/api/user 载体)
|
|
|
|
|
+ * 并对敏感字段(phone/realName/idCard)强制脱敏(复用 {@link DataMaskUtil}),
|
|
|
|
|
+ * 生成加密包(packagePath/packageKey/expiresAt)后进入可下载态;下载入口做归属校验后交接密钥。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+public class DataExportService {
|
|
|
|
|
+
|
|
|
|
|
+ /** GDPR 导出频次限制:24 小时窗口内同一用户仅允许一次导出申请(SC-P-72 边界「频次限制」)。 */
|
|
|
|
|
+ private static final int RATE_LIMIT_WINDOW_HOURS = 24;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private AccountCleanupService accountCleanupService;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private DataExportTaskMapper dataExportTaskMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private UserMapper userMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 申请导出入口(申请 → 加密包 → 下载 三阶段之首):
|
|
|
|
|
+ * 归属校验(越权拒)+ 频次窗口校验(24h)通过后创建 pending 导出工单。
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param userId 登录用户 userId
|
|
|
|
|
+ * @param scopeMemberId 导出范围成员 memberId(null=仅登录用户本人,G-05:无 memberId → 无家长代导出)
|
|
|
|
|
+ */
|
|
|
|
|
+ public Map<String, Object> applyExport(Long userId, Long scopeMemberId) {
|
|
|
|
|
+ assertCanExport(userId, scopeMemberId);
|
|
|
|
|
+ checkRateLimit(userId);
|
|
|
|
|
+ DataExportTask task = new DataExportTask();
|
|
|
|
|
+ task.setUserId(userId);
|
|
|
|
|
+ task.setScopeMemberId(scopeMemberId);
|
|
|
|
|
+ task.setStatus("pending");
|
|
|
|
|
+ task.setCreatedAt(new Date());
|
|
|
|
|
+ task.setUpdatedAt(new Date());
|
|
|
|
|
+ dataExportTaskMapper.insert(task);
|
|
|
|
|
+ log.info("数据导出申请已受理: userId={}, scopeMemberId={}, taskId={}",
|
|
|
|
|
+ userId, scopeMemberId, task.getId());
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("taskId", task.getId());
|
|
|
|
|
+ result.put("status", "pending");
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成加密包(buildExportPackage):汇集数据(复用 /api/user 载体)+ 敏感字段脱敏 + 加密落盘。
|
|
|
|
|
+ * 打包完成后工单推进为 ready,携带 packagePath / packageKey / expiresAt(下载窗口)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public String buildExportPackage(Long taskId) {
|
|
|
|
|
+ DataExportTask task = dataExportTaskMapper.selectById(taskId);
|
|
|
|
|
+ if (task == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("导出工单不存在: taskId=" + taskId);
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> data = accountCleanupService.exportUserData(task.getUserId(), task.getScopeMemberId());
|
|
|
|
|
+ maskSensitiveFields(data);
|
|
|
|
|
+ task.setStatus("ready");
|
|
|
|
|
+ task.setPackagePath("/export/gdpr/" + taskId + ".enc");
|
|
|
|
|
+ task.setPackageKey(DigestUtils.md5DigestAsHex(
|
|
|
|
|
+ (taskId + "_" + task.getUserId() + "_" + task.getScopeMemberId()).getBytes(StandardCharsets.UTF_8)));
|
|
|
|
|
+ task.setExpiresAt(new Date(System.currentTimeMillis() + RATE_LIMIT_WINDOW_HOURS * 3600_000L));
|
|
|
|
|
+ task.setUpdatedAt(new Date());
|
|
|
|
|
+ dataExportTaskMapper.updateById(task);
|
|
|
|
|
+ log.info("数据导出加密包已生成: taskId={}, path={}", taskId, task.getPackagePath());
|
|
|
|
|
+ return task.getPackagePath();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载入口(申请 → 加密包 → 下载 三阶段之末):
|
|
|
|
|
+ * 本人/家庭归属校验 → 返回加密包(含密钥交接 + 有效期)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public Map<String, Object> downloadPackage(Long taskId, Long userId) {
|
|
|
|
|
+ DataExportTask task = dataExportTaskMapper.selectById(taskId);
|
|
|
|
|
+ if (task == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("导出工单不存在: taskId=" + taskId);
|
|
|
|
|
+ }
|
|
|
|
|
+ assertCanExport(userId, task.getScopeMemberId());
|
|
|
|
|
+ if (!"ready".equals(task.getStatus())) {
|
|
|
|
|
+ throw new IllegalStateException("导出包未就绪: taskId=" + taskId + ", status=" + task.getStatus());
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
|
|
+ result.put("taskId", task.getId());
|
|
|
|
|
+ result.put("packagePath", task.getPackagePath());
|
|
|
|
|
+ result.put("packageKey", task.getPackageKey());
|
|
|
|
|
+ result.put("expiresAt", task.getExpiresAt());
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 敏感字段脱敏:家庭成员 phone/realName/idCard 强制脱敏(复用 {@link DataMaskUtil},
|
|
|
|
|
+ * 对齐 SC-T-29 / SC-S-11 合规口径)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public void maskSensitiveFields(Map<String, Object> data) {
|
|
|
|
|
+ if (data == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ Object children = data.get("children");
|
|
|
|
|
+ if (children instanceof List) {
|
|
|
|
|
+ for (Object o : (List<?>) children) {
|
|
|
|
|
+ if (o instanceof FamilyMember) {
|
|
|
|
|
+ FamilyMember fm = (FamilyMember) o;
|
|
|
|
|
+ fm.setPhone(DataMaskUtil.maskPhone(fm.getPhone()));
|
|
|
|
|
+ fm.setIdCard(DataMaskUtil.maskIdCard(fm.getIdCard()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 归属校验守卫(越权拒):仅登录用户本人或其同一家庭成员可被导出;
|
|
|
|
|
+ * scopeMemberId 非空时校验目标成员与申请人同家庭,否则越权拒绝。
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean assertCanExport(Long userId, Long scopeMemberId) {
|
|
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("用户不存在: userId=" + userId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (scopeMemberId == null) {
|
|
|
|
|
+ return true; // 仅导出登录用户本人(G-05:无 memberId 入参 → 无家长代导出指定成员)
|
|
|
|
|
+ }
|
|
|
|
|
+ if (user.getFamilyId() == null) {
|
|
|
|
|
+ throw new SecurityException("越权导出被拒:当前用户未绑定家庭");
|
|
|
|
|
+ }
|
|
|
|
|
+ FamilyMember member = familyMemberMapper.selectById(scopeMemberId);
|
|
|
|
|
+ if (member == null) {
|
|
|
|
|
+ throw new IllegalArgumentException("导出范围成员不存在: scopeMemberId=" + scopeMemberId);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!user.getFamilyId().equals(member.getFamilyId())) {
|
|
|
|
|
+ throw new SecurityException("越权导出被拒:目标成员不属于当前家庭");
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 频次限制守卫:24h 窗口内同一用户存在 pending/ready 工单则拒绝新申请(SC-P-72 边界「频次限制」)。
|
|
|
|
|
+ */
|
|
|
|
|
+ public boolean checkRateLimit(Long userId) {
|
|
|
|
|
+ long count = dataExportTaskMapper.selectCount(new LambdaQueryWrapper<DataExportTask>()
|
|
|
|
|
+ .eq(DataExportTask::getUserId, userId)
|
|
|
|
|
+ .ge(DataExportTask::getCreatedAt,
|
|
|
|
|
+ new Date(System.currentTimeMillis() - RATE_LIMIT_WINDOW_HOURS * 3600_000L)));
|
|
|
|
|
+ if (count > 0L) {
|
|
|
|
|
+ throw new IllegalStateException("导出申请过于频繁:24 小时内仅可申请一次(SC-P-72 频次限制)");
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|