获取客户端IP和MAC地址
小編是菜鳥,這兩天拿到一個需求,登錄時判斷ip地址是否允許登錄,這幾天做下來小編遇見了好多坑,給大家分享一下。
系統架構:.net+java
首先,不管怎么樣http請求頭才是獲取ip地址的唯一方式:
1、js是一個腳本語言,不能獲取本機ip地址;
2、java可以直接獲取請求,但是.net是要在iss上部署,如果直接在后端獲取,獲取的是部署機器上的ip地址,只能在.net部分獲取;
話不多少,給大家分享一下源碼;
java部分:
import javax.servlet.http.HttpServletRequest;
public class GetIp {
public static String getUserIp(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;
}
}
.net部分:
public string GetIP()
{
HttpRequest request = HttpContext.ApplicationInstance.Context.Request;
string ip = request.ServerVariables[“HTTP_X_FORWARDED_FOR”];
if (string.IsNullOrEmpty(ip)) {
ip = request.ServerVariables[“REMOTE_ADDR”];
}
if (string.IsNullOrEmpty(ip)) {
ip = request.UserHostAddress;
}
if (string.IsNullOrEmpty(ip)) {
ip = “0.0.0.0”;
}
return ip;
}
大家有什么不懂,可以直接聯系小編,小編第一時間為你解答!
寫完這個需求,客戶又該需求了,讓使用MAC地址驗證,下來在講講MAC地址的獲取:
package com.asppro.util;import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern;public class GetAddress {/*** 獲取客戶端ip地址* @param request* @return*/public static String getUserIp(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;}/**獲取本地的mac地址* @param args* @throws UnknownHostException * @throws SocketException */public static String getLocalMac() throws SocketException, UnknownHostException {// TODO Auto-generated method stubInetAddress ia = InetAddress.getLocalHost();//獲取網卡,獲取地址byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();StringBuffer sb = new StringBuffer("");for(int i=0; i<mac.length; i++) {if(i!=0) {sb.append("-");}//字節轉換為整數int temp = mac[i]&0xff;String str = Integer.toHexString(temp);if(str.length()==1) {sb.append("0"+str);}else {sb.append(str);}}return sb.toString();}/*** 通過ip地址獲取客戶端的mac地址* @param ip* @return* @throws Exception*/public static String GetMacAddress(String ip) throws Exception {if(!booleanIP(ip)){return "IP非法";}if(ip.equalsIgnoreCase("127.0.0.1")){return "不能輸入本地地址";}String str = "";String macAddress = "";try {if (!Ping(ip)) {return "無法訪問";}Process p = Runtime.getRuntime().exec("arp -a ");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(ip) > 1) {str = str.replace(ip, "").replaceAll(" ", "");if(str.length()>17){macAddress = str.substring(0, 17);}break;}}}} catch (IOException e) {e.printStackTrace(System.out);}return macAddress;}/*** 驗證IP是否非法* @param ip* @return*/private static boolean booleanIP(String ip) {String p = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";Pattern pattern = Pattern.compile(p);Matcher matcher = pattern.matcher(ip);return matcher.matches();}/*** 判斷ip通信是否通暢* @param ip* @return* @throws IOException*/private static boolean Ping(String ip) throws Exception {boolean f = false;Process process = Runtime.getRuntime().exec("ping " + ip);InputStreamReader r = new InputStreamReader(process.getInputStream());LineNumberReader returnData = new LineNumberReader(r);String line = "";while ((line = returnData.readLine()) != null) {if(line.indexOf(String.valueOf("TTL")) == -1){}else {f = true;break;}}return f;} }這是獲取MAC地址的方法,有兩個注意的地方:
1、獲取MAC地址的電腦必須能夠ping通;
2、不能使用localhost和127.0.0.1獲取;
還有一個獲取本地IP地址的方法,也在這里總結一下:
String localIp = InetAddress.getLocalHost().getHostAddress();
總結
以上是生活随笔為你收集整理的获取客户端IP和MAC地址的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Flex+ActionScript
- 下一篇: oracle主键自动增长