Java如何獲取客戶端mac地址
Java獲取客戶端mac地址
問題
項(xiàng)目中需要實(shí)現(xiàn)一個(gè)功能,在用戶登錄的時(shí)候,要求系統(tǒng)賬號(hào)和計(jì)算機(jī)綁定,只有綁定的賬號(hào)才可以登錄,并且每個(gè)賬號(hào)只能綁定一臺(tái)計(jì)算機(jī)。
解決方案
通過請(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)卡問題,截取緊靠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)文章
SpringBoot項(xiàng)目使用協(xié)同過濾的實(shí)現(xiàn)
協(xié)同過濾是一種常用的推薦系統(tǒng)算法,用于預(yù)測(cè)用戶可能喜歡的物品,本文主要介紹了SpringBoot項(xiàng)目使用協(xié)同過濾的實(shí)現(xiàn),感興趣的可以了解一下2023-09-09
Spring boot 數(shù)據(jù)源未配置異常的解決
這篇文章主要介紹了Spring boot 數(shù)據(jù)源未配置異常的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java如何把數(shù)組轉(zhuǎn)換為ArrayList
這篇文章主要介紹了Java如何把數(shù)組轉(zhuǎn)換為ArrayList,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Windows環(huán)境下重啟jar服務(wù)bat代碼的解決方案
在Windows環(huán)境下部署java的jar包,若有多個(gè)服務(wù)同時(shí)啟動(dòng),很難找到相應(yīng)服務(wù)重啟,每次都重啟全部服務(wù)很麻煩,應(yīng)用場(chǎng)景大多用于部署測(cè)試,今天給大家分享Windows環(huán)境下重啟jar服務(wù)bat代碼,感興趣的朋友一起看看吧2023-08-08
Spring Boot 集成Shiro的多realm配置過程
這篇文章主要介紹了Spring Boot 集成Shiro的多realm配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Spring Security添加驗(yàn)證碼的兩種方式小結(jié)
使用spring security的時(shí)候,框架會(huì)幫我們做賬戶密碼的驗(yàn)證,但是如我們需要添加一個(gè)驗(yàn)證碼,就需要對(duì)配置文件進(jìn)行修改,這篇文章主要給大家介紹了關(guān)于Spring Security添加驗(yàn)證碼的兩種方式,需要的朋友可以參考下2021-10-10
詳解java操作Redis數(shù)據(jù)庫的redis工具(RedisUtil,jedis工具JedisUtil,JedisPoo
這篇文章主要介紹了java操作Redis數(shù)據(jù)庫的redis工具,包括RedisUtil,jedis工具JedisUtil,JedisPoolUtil工具,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08

