構建 Web 手機簽到系統,需防止代簽。要求:學生連指定 WiFi 後訪問局域網 IP 簽到,後台識別手機唯一標識(MAC/IP),並判斷客戶端操作系統類型以防電腦模擬。
String ip = request.getRemoteAddr();arp 命令解析輸出:String macAdd = "";
String arpCMD = "arp -a ";
try {
Process process = Runtime.getRuntime().exec(arpCMD + ip);
InputStreamReader isr = new InputStreamReader(process.getInputStream());
LineNumberReader reader = new LineNumberReader(isr);
String line;
while ((line = reader.readLine()) != null) {
if (line.indexOf(ip) >= 0) {
// 解析 "at" 後的 MAC 片段(視具體輸出格式調整索引)
int atIdx = line.indexOf("at");
if (atIdx > 0) {
macAdd = line.substring(atIdx + 3, atIdx + 20).trim();
}
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return macAdd;
僅需將命令改為 arp -n,解析邏輯類似。
通過 User-Agent 頭識別:
String ua = request.getHeader("User-Agent").toLowerCase();
boolean isMobile = ua.contains("android") || ua.contains("iphone") || ua.contains("mobile");
boolean isPC = ua.contains("windows") || ua.contains("macintosh") || ua.contains("linux x86");
arp 權限(通常需 root/Administrator)。ping 一下客戶端 IP 刷新 ARP 表。| 方案 | 優點 | 缺點 |
|---|---|---|
| ARP 反查 MAC | 無需客戶端配合,適用現有網絡 | 依賴同網段、需高權限、Linux/Win 命令差異 |
| WebRTC ICE 候選 | 瀏覽器原生支持 | 用戶可禁用、僅獲取本地 IP |
| App 端主動上報 | 可獲取 IMEI/Android ID 等 | 需安裝 App、iOS 限制嚴格 |
綜合考量,ARP 方案在「純 Web + 局域網 + 已有 WiFi 控制」場景下工程成本最低,但需做好異常處理與降級策略。