From 51f65ef59d2a10449f02ccdd55984f75c022cfc7 Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Fri, 21 Nov 2025 11:40:51 +0800
Subject: [PATCH] 优化机器码获取

---
 ard-system/src/main/java/com/ard/system/authorize/common/AuthorizeUtil.java |   44 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/ard-system/src/main/java/com/ard/system/authorize/common/AuthorizeUtil.java b/ard-system/src/main/java/com/ard/system/authorize/common/AuthorizeUtil.java
index dc8b641..8fe1cc2 100644
--- a/ard-system/src/main/java/com/ard/system/authorize/common/AuthorizeUtil.java
+++ b/ard-system/src/main/java/com/ard/system/authorize/common/AuthorizeUtil.java
@@ -76,7 +76,7 @@
         return null;
     }
 
-    public static String getMachineCode() {
+    public static String getMachineCode1() {
         StringBuffer sb = new StringBuffer();
         try {
             InetAddress ia = InetAddress.getLocalHost();
@@ -104,6 +104,48 @@
         log.debug("机器码:"+code);
         return code;
     }
+    public static String getMachineCode() {
+        try {
+            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+            while (interfaces.hasMoreElements()) {
+                NetworkInterface nif = interfaces.nextElement();
+
+                // 跳过回环、虚拟、未启动网卡
+                if (nif.isLoopback() || nif.isVirtual() || !nif.isUp()) {
+                    continue;
+                }
+
+                // 可选:跳过 idrac 管理网卡
+                if (nif.getName().toLowerCase().contains("idrac")) {
+                    continue;
+                }
+
+                byte[] mac = nif.getHardwareAddress();
+                if (mac == null || mac.length == 0) {
+                    continue;
+                }
+
+                StringBuilder sb = new StringBuilder();
+                for (int i = 0; i < mac.length; i++) {
+                    if (i != 0) sb.append("-");
+                    sb.append(String.format("%02X", mac[i]));
+                }
+
+                String macStr = sb.toString();
+                log.debug("物理网卡: " + nif.getName() + " MAC: " + macStr);
+
+                // 返回 MAC 的 MD5 作为机器码
+                String code = Md5Utils.hash(macStr);
+                log.debug("机器码: " + code);
+                return code;
+            }
+        } catch (Exception e) {
+            log.error("获取机器码异常: " + e.getMessage(), e);
+        }
+
+        // 找不到 MAC 返回 null
+        return null;
+    }
 
     public static String getCPUCode() {
         String serial = null;

--
Gitblit v1.9.3