欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java如何獲取客戶端mac地址

 更新時(shí)間:2024年09月27日 10:15:11   作者:岸遠(yuǎn)水聲微  
在用戶登錄時(shí),通過(guò)獲取IP地址來(lái)識(shí)別計(jì)算機(jī)的MAC地址,然后將用戶賬號(hào)與該MAC地址進(jìn)行綁定,確保每個(gè)賬號(hào)只能在一臺(tái)特定的計(jì)算機(jī)上登錄,增強(qiáng)系統(tǒng)安全性,這種方法適用于需要嚴(yán)格賬戶安全管理的場(chǎng)景

Java獲取客戶端mac地址

問(wèn)題

項(xiàng)目中需要實(shí)現(xiàn)一個(gè)功能,在用戶登錄的時(shí)候,要求系統(tǒng)賬號(hào)和計(jì)算機(jī)綁定,只有綁定的賬號(hào)才可以登錄,并且每個(gè)賬號(hào)只能綁定一臺(tái)計(jì)算機(jī)。

解決方案

通過(guò)請(qǐng)求IP獲取mac地址,然后將賬號(hào)與mac地址進(jìn)行綁定。

代碼實(shí)現(xiàn)如下:

String getMacInfo(HttpServletRequest request)
    {
        //獲取ip地址
        String macInfo = null;
        try
        {
            String ip = request.getRemoteAddr();
            //linux下獲取mac地址
            macAddr = CommonUtils.getMac(ip);
            //windows下獲取mac地址
            if(StringUtils.isBlank(macAddr)){
                macAddr = CommonUtils.getMacInWindows(ip).trim();
            }
        }
        catch (Exception e)
        {
            log.error("獲取mac地址失敗");
            return null;
        }
        return macInfo;
    }

CommonUtils.java

	// 從類unix機(jī)器上獲取mac地址
	 public static String getMac(String ip) throws IOException {
        String mac = SysCode.BDFH.EMPTY;
        if (ip != null) {
            try {
                Process process = Runtime.getRuntime().exec("arp "+ip);
                InputStreamReader ir = new InputStreamReader(process.getInputStream());
                LineNumberReader input = new LineNumberReader(ir);
                String line;
                StringBuffer s = new StringBuffer();
                while ((line = input.readLine()) != null) {
                    s.append(line);
                }
                mac = s.toString();
                if (StringUtils.isNotBlank(mac)) {
                    mac = mac.substring(mac.indexOf(":") - 2, mac.lastIndexOf(":") + 3);
                }
                return mac;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return mac;
    }
    
    // 從windows機(jī)器上獲取mac地址
	public static String getMacInWindows(final String ip) {
        String result = "";
        String[] cmd = {"cmd", "/c", "ping " + ip};
        String[] another = {"cmd", "/c", "ipconfig -all"};
        // 獲取執(zhí)行命令后的result
        String cmdResult = callCmd(cmd, another);
        // 從上一步的結(jié)果中獲取mac地址
        result = filterMacAddress(ip, cmdResult, "-");
        return result;
 	}
 	
 	// 命令執(zhí)行
 	public static String callCmd(String[] cmd, String[] another) {
        String result = "";
        String line = "";
        try {
            Runtime rt = Runtime.getRuntime();
            // 執(zhí)行第一個(gè)命令
            Process proc = rt.exec(cmd);
            proc.waitFor();
             // 執(zhí)行第二個(gè)命令
            proc = rt.exec(another);
            InputStreamReader is = new InputStreamReader(proc.getInputStream());
            BufferedReader br = new BufferedReader(is);
            while ((line = br.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
	
	// 獲取mac地址
	public static String filterMacAddress(final String ip, final String sourceString, final String macSeparator) {
        String result = "";
        String regExp = "((([0-9,A-F,a-f]{1,2}" + macSeparator + "){1,5})[0-9,A-F,a-f]{1,2})";
        Pattern pattern = Pattern.compile(regExp);
        Matcher matcher = pattern.matcher(sourceString);
        while (matcher.find()) {
            result = matcher.group(1);
            // 因計(jì)算機(jī)多網(wǎng)卡問(wèn)題,截取緊靠IP后的第一個(gè)mac地址
            int num = sourceString.indexOf(ip) - sourceString.indexOf(": "+result + " ");
            if (num>0&&num<300) {
                break;
            }
        }
        return result;
    }

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論