liusuyi
2025-11-21 51f65ef59d2a10449f02ccdd55984f75c022cfc7
优化机器码获取
1 files modified
44 ■■■■■ changed files
ard-system/src/main/java/com/ard/system/authorize/common/AuthorizeUtil.java 44 ●●●●● patch | view | raw | blame | history
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;