JSF/JAVA 根據IP獲取客戶端Mac地址
需要對使用者的 ip 和 mac 地址進行驗證,這裡用到獲取客戶端ip和mac地址的兩個方法,留存。
1.獲取客戶端ip地址( 這個必須從客戶端傳到後臺):
jsp頁面下,很簡單,request.getRemoteAddr() ;
因為系統的VIew層是用JSF來實現的,因此頁面上沒法直接獲得類似request,在bean裡做了個強制轉換
- public String getMyIP() {
- try {
- FacesContext fc = FacesContext.getCurrentInstance();
- HttpServletRequest request = (HttpServletRequest)fc.getExternalContext().getRequest();
- return request.getRemoteAddr();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return "";
- }
2.獲取客戶端mac地址
呼叫window的命令,在後臺Bean裡實現 通過ip來獲取mac地址。方法如下:
- public String getMACAddress(String ip){
- String str = "";
- String macAddress = "";
- try {
- Process p = Runtime.getRuntime().exec("nbtstat -A " + ip);
- InputStreamReader ir = new InputStreamReader(p.getInputStream());
- LineNumberReader input = new LineNumberReader(ir);
- for (int i = 1; i < 100; i++) {
- str = input.readLine();
- if (str != null) {
- if (str.indexOf("MAC Address") > 1) {
- macAddress = str.substring(str.indexOf("MAC Address") + 14, str.length());
- break;
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace(System.out);
- }
- return macAddress;
- }
完整程式碼:
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- public class GetMACAddress {
- public String getMACAddress(String ipAddress) {
- String str = "", strMAC = "", macAddress = "";
- try {
- Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
- InputStreamReader ir = new InputStreamReader(pp.getInputStream());
- LineNumberReader input = new LineNumberReader(ir);
- for (int i = 1; i < 100; i++) {
- str = input.readLine();
- if (str != null) {
- if (str.indexOf("MAC Address") > 1) {
- strMAC = str.substring(str.indexOf("MAC Address") + 14,
- str.length());
- break;
- }
- }
- }
- } catch (IOException ex) {
- return "Can't Get MAC Address!";
- }
- //
- if (strMAC.length() < 17) {
- return "Error!";
- }
- macAddress = strMAC.substring(0, 2) + ":" + strMAC.substring(3, 5)
- + ":" + strMAC.substring(6, 8) + ":" + strMAC.substring(9, 11)
- + ":" + strMAC.substring(12, 14) + ":"
- + strMAC.substring(15, 17);
- //
- return macAddress;
- }
- public static void main(String[] args) {
- GetMACAddress getMACAddress = new GetMACAddress();
- System.out.println(getMACAddress.getMACAddress("59.78.63.38")); //獲得該ip地址的mac地址
- }
- public static String procAll(String str) {
- return procStringEnd(procFirstMac(procAddress(str)));
- }
- public static String procAddress(String str) {
- int indexof = str.indexOf("Physical Address");
- if (indexof > 0) {
- return str.substring(indexof, str.length());
- }
- return str;
- }
- public static String procFirstMac(String str) {
- int indexof = str.indexOf(":");
- if (indexof > 0) {
- return str.substring(indexof + 1, str.length()).trim();
- }
- return str;
- }
- public static String procStringEnd(String str) {
- int indexof = str.indexOf("\r");
- if (indexof > 0) {
- return str.substring(0, indexof).trim();
- }
- return str;
- }
- }
只要寫一個servlet來呼叫這個類的getMACAddress(String netip)方法就可以獲得客戶端的mac地址了,相信你會寫jsp應該對servlet也不陌生吧,在jsp中呼叫servlet通過session傳遞返回的mac地址,加以判斷就可以了
mac地址是可以通過登錄檔修改的,不建議以此來作為限制依據~
補充:
關於獲取IP地址的方式,最近在linux下有一個教訓,如果單純通過InetAddress來獲取IP地址,就會出現在不同的機器上IP地址不同的問題。
InetAddress.getLocalHost().getAddress() 實際上是根據hostname來獲取IP地址的。linux系統在剛剛裝完預設的hostname是localhost,所以通過上面程式碼獲取到的本機 ip就是127.0.0.1, 相對應,比如我的hostname就是rjlin.atsig.com 返回的ip地址確是atsig.com的地址。暫時採用下面程式碼來處理,當然還不夠靈活:
public static byte[] getIp() throws UnknownHostException {
byte[] b = InetAddress.getLocalHost().getAddress();
Enumeration allNetInterfaces = null;
try {
allNetInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
e.printStackTrace();
}
InetAddress ip = null;
NetworkInterface netInterface = null;
while (allNetInterfaces.hasMoreElements()) {
netInterface = (NetworkInterface) allNetInterfaces.nextElement();
if (netInterface.getName().trim().equals("eth0")){
Enumeration addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
}
break;
}
}
if (ip != null && ip instanceof Inet4Address) {
return b = ip.getAddress();
}
return b;
}
補充:
// 獲取真實IP的方法().
public String getIpAddr(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
http://justsee.iteye.com/blog/803316
相關文章
- 獲取客戶端Mac地址客戶端Mac
- 服務端如何獲取客戶端請求IP地址服務端客戶端
- 在OwinSelfHost專案中獲取客戶端IP地址客戶端
- 在SelfHost專案中獲取客戶端IP地址客戶端
- js根據IP地址獲取當前的省市JS
- js根據ip地址獲取省份城市的方法JS
- Spring 客戶端 IP 地址獲取及儲存細節Spring客戶端
- js根據ip地址獲取城市地理位置JS
- java web 通過request獲取客戶端IPJavaWeb客戶端
- c# 獲取客戶端IPC#客戶端
- 獲取客戶端真實IP客戶端
- .net 獲取客戶端真實ip客戶端
- 根據 IP 獲取省市名稱
- Java面試題-如何獲取客戶端真實IPJava面試題客戶端
- spring boot 獲取客戶端ip資訊Spring Boot客戶端
- Django透過request獲取客戶端IPDjango客戶端
- 伺服器獲取真實客戶端 IP伺服器客戶端
- 一次獲取客戶端 IP 記錄客戶端
- C#根據經緯度獲取實體地址C#
- java獲取本機的ip地址Java
- 阿里雲CDN + nginx多級代理獲取客戶端IP阿里Nginx客戶端
- Nginx 反向代理後如何獲取真實客戶端 IPNginx客戶端
- nginx多級代理下如何獲取客戶端真實IPNginx客戶端
- 在容器服務中獲取客戶端真實源 IP客戶端
- nginx反向代理獲取客戶端的真實IP和域名Nginx客戶端
- ABP vNext 審計日誌獲取真實客戶端IP客戶端
- saltstack獲取IP地址
- 爬蟲實現:根據IP地址反查域名爬蟲
- Nacos - 客戶端例項列表獲取客戶端
- 乾貨:不同場景容器內獲取客戶端源IP的方法客戶端
- jQuery獲取本機ip地址jQuery
- 如何獲取海外住宅IP地址?
- 美國ip地址如何獲取?
- 使用linux三劍客取ip地址Linux
- 根據使用者來獲取渠道
- Python 基礎練習 —— 獲取本機 Mac 地址、ip 地址和主機名PythonMac
- js根據經緯度,獲取省市區。(百度地圖逆地址解析)JS地圖
- Jaeger的客戶端取樣配置(Java版)客戶端Java
- 獲取IP地址的途徑有哪些?要如何保護IP地址不被竊取?