From dca8ba79f1334a1910e01b8e6d121611c9656fa2 Mon Sep 17 00:00:00 2001 From: tianfeng <769204422@qq.com> Date: Tue, 30 Dec 2025 19:58:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=96=B0=E7=89=88=E6=8C=87?= =?UTF-8?q?=E7=BA=B9ip=E9=99=90=E5=88=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../device/LatestMobileDeviceService.java | 11 +++- .../impl/LatestMobileDeviceServiceImpl.java | 65 ++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/LatestMobileDeviceService.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/LatestMobileDeviceService.java index 649ad0ab..75b7529b 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/LatestMobileDeviceService.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/LatestMobileDeviceService.java @@ -109,13 +109,22 @@ public interface LatestMobileDeviceService extends BaseService getImeiUserId(Long userId); /** - * 获取用户设备指纹 + * 获取用户设备指纹(旧版) * * @param userId 用户ID * @return 设备指纹字符串 */ String getDeviceFingerprintByUserId(Long userId); + /** + * 获取用户设备指纹(增强版) + * 包含:手机型号 + 系统版本 + IMEI + IP段 + 编译版本 + * + * @param userId 用户ID + * @return 设备指纹MD5字符串 + */ + String getEnhancedDeviceFingerprintByUserId(Long userId); + /** * 根据设备指纹获取所有使用该指纹的用户ID列表 * diff --git a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/impl/LatestMobileDeviceServiceImpl.java b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/impl/LatestMobileDeviceServiceImpl.java index f29a6f0b..1ea800e6 100644 --- a/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/impl/LatestMobileDeviceServiceImpl.java +++ b/rc-service/rc-service-other/other-infrastructure/src/main/java/com/red/circle/other/infra/database/rds/service/user/device/impl/LatestMobileDeviceServiceImpl.java @@ -12,6 +12,9 @@ import com.red.circle.other.infra.database.rds.dao.user.device.LatestMobileDevic import com.red.circle.other.infra.database.rds.entity.user.user.LatestMobileDevice; import com.red.circle.other.infra.database.rds.service.user.device.LatestMobileDeviceService; import com.red.circle.other.inner.model.cmd.user.device.PageLatestMobileDeviceQryCmd; +import org.springframework.util.DigestUtils; + +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; import java.util.Objects; @@ -297,8 +300,27 @@ public class LatestMobileDeviceServiceImpl extends ); } + @Override + public String getEnhancedDeviceFingerprintByUserId(Long userId) { + if (userId == null) { + return ""; + } + + LatestMobileDevice device = query() + .eq(LatestMobileDevice::getUserId, userId) + .orderByDesc(LatestMobileDevice::getCreateTime) + .last(PageConstant.LIMIT_ONE) + .getOne(); + + if (device == null) { + return ""; + } + + return buildEnhancedDeviceFingerprint(device); + } + /** - * 构建设备指纹 + * 构建设备指纹(旧版) */ private String buildDeviceFingerprint(String requestClient, String phoneModel, String ip) { @@ -311,6 +333,47 @@ public class LatestMobileDeviceServiceImpl extends return requestClient + "|" + phoneModel + "|" + ip; } + /** + * 构建增强版设备指纹 + * 组成:手机型号 + 系统版本 + IMEI + IP段 + 编译版本 + */ + private String buildEnhancedDeviceFingerprint(LatestMobileDevice device) { + StringBuilder sb = new StringBuilder(); + + sb.append(StringUtils.isBlank(device.getPhoneModel()) ? "" : device.getPhoneModel()).append("|"); + sb.append(StringUtils.isBlank(device.getPhoneSysVersion()) ? "" : device.getPhoneSysVersion()).append("|"); + sb.append(StringUtils.isBlank(device.getImei()) ? "" : device.getImei()).append("|"); + sb.append(getIpPrefix(device.getIp())).append("|"); + sb.append(Objects.nonNull(device.getBuildVersion()) ? device.getBuildVersion() : 0); + + return DigestUtils.md5DigestAsHex(sb.toString().getBytes(StandardCharsets.UTF_8)); + } + + /** + * 获取IP前缀段(提高稳定性) + * IPv4: 取前3段(如 192.168.1.xxx -> 192.168.1) + * IPv6: 取前4段(如 2a0d:5600:24:449:xxx -> 2a0d:5600:24:449) + */ + private String getIpPrefix(String ip) { + if (StringUtils.isBlank(ip)) { + return ""; + } + + if (ip.contains(":")) { + String[] parts = ip.split(":"); + if (parts.length >= 4) { + return parts[0] + ":" + parts[1] + ":" + parts[2] + ":" + parts[3]; + } + return ip; + } else { + String[] parts = ip.split("\\."); + if (parts.length >= 3) { + return parts[0] + "." + parts[1] + "." + parts[2]; + } + return ip; + } + } + @Override public List listUserIdsByDeviceFingerprint(String fingerprint, String sysOrigin) { if (StringUtils.isBlank(fingerprint) || StringUtils.isBlank(sysOrigin)) {