获取客户端IP和MAC地址
小编是菜鸟,这两天拿到一个需求,登录时判断ip地址是否允许登录,这几天做下来小编遇见了好多坑,给大家分享一下。系统架构:.net+java首先,不管怎么样http请求头才是获取ip地址的唯一方式:1、js是一个脚本语言,不能获取本机ip地址;2、java可以直接获取请求,但是.net是要在iss上部署,如果直接在后端获取,获取的是部署机器上的ip地址,只能在...
小编是菜鸟,这两天拿到一个需求,登录时判断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 stub
InetAddress 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();
更多推荐
所有评论(0)