java

位置:IT落伍者 >> java >> 浏览文章

JSF/JAVA根据IP获取客户端Mac地址


发布日期:2022年09月04日
 
JSF/JAVA根据IP获取客户端Mac地址

需要对用户的 ip 和 mac 地址进行验证这里用到获取客户端ip和mac地址的两个方法留存

获取客户端ip地址( 这个必须从客户端传到后台)

jsp页面下很简单requestgetRemoteAddr() ;

因为系统的VIew层是用JSF来实现的因此页面上没法直接获得类似request在bean里做了个强制转换

Java代码

public String getMyIP() {

try {

FacesContext fc = FacesContextgetCurrentInstance();

HttpServletRequest request = (HttpServletRequest)fcgetExternalContext()getRequest();

return requestgetRemoteAddr();

}

catch (Exception e) {

eprintStackTrace();

}

return ;

}

获取客户端mac地址

调用window的命令在后台Bean里实现 通过ip来获取mac地址方法如下

Java代码

public String getMACAddress(String ip){

String str = ;

String macAddress = ;

try {

Process p = RuntimegetRuntime()exec(nbtstat A + ip);

InputStreamReader ir = new InputStreamReader(pgetInputStream());

LineNumberReader input = new LineNumberReader(ir);

for (int i = ; i < ; i++) {

str = inputreadLine();

if (str != null) {

if (strindexOf(MAC Address) > ) {

macAddress = strsubstring(strindexOf(MAC Address) + strlength());

break;

}

}

}

} catch (IOException e) {

eprintStackTrace(Systemout);

}

return macAddress;

}

完整代码

Java代码

import javaioIOException;

import javaioInputStreamReader;

import javaioLineNumberReader;

public class GetMACAddress {

public String getMACAddress(String ipAddress) {

String str = strMAC = macAddress = ;

try {

Process pp = RuntimegetRuntime()exec(nbtstat a + ipAddress);

InputStreamReader ir = new InputStreamReader(ppgetInputStream());

LineNumberReader input = new LineNumberReader(ir);

for (int i = ; i < ; i++) {

str = inputreadLine();

if (str != null) {

if (strindexOf(MAC Address) > ) {

strMAC = strsubstring(strindexOf(MAC Address) +

strlength());

break;

}

}

}

} catch (IOException ex) {

return Cant Get MAC Address!;

}

//

if (strMAClength() < ) {

return Error!;

}

macAddress = strMACsubstring( ) + : + strMACsubstring( )

+ : + strMACsubstring( ) + : + strMACsubstring( )

+ : + strMACsubstring( ) + :

+ strMACsubstring( );

//

return macAddress;

}

public static void main(String[] args) {

GetMACAddress getMACAddress = new GetMACAddress();

Systemoutprintln(getMACAddressgetMACAddress()); //获得该ip地址的mac地址

}

public static String procAll(String str) {

return procStringEnd(procFirstMac(procAddress(str)));

}

public static String procAddress(String str) {

int indexof = strindexOf(Physical Address);

if (indexof > ) {

return strsubstring(indexof strlength());

}

return str;

}

public static String procFirstMac(String str) {

int indexof = strindexOf(:);

if (indexof > ) {

return strsubstring(indexof + strlength())trim();

}

return str;

}

public static String procStringEnd(String str) {

int indexof = strindexOf(\r);

if (indexof > ) {

return strsubstring( indexof)trim();

}

return str;

}

}

只要写一个servlet来调用这个类的getMACAddress(String netip)方法就可以获得客户端的mac地址了相信你会写jsp应该对servlet也不陌生吧在jsp中调用servlet通过session传递返回的mac地址加以判断就可以了

mac地址是可以通过注册表修改的不建议以此来作为限制依据~

补充

关于获取IP地址的方式最近在linux下有一个教训如果单纯通过InetAddress来获取IP地址就会出现在不同的机器上IP地址不同的问题

InetAddressgetLocalHost()getAddress() 实际上是根据hostname来获取IP地址的linux系统在刚刚装完默认的hostname是localhost所以通过上面代码获取到的本机 ip就是 相对应比如我的hostname就是 返回的ip地址确是的地址暂时采用下面代码来处理当然还不够灵活

public static byte[] getIp() throws UnknownHostException {

byte[] b = InetAddressgetLocalHost()getAddress();

Enumeration allNetInterfaces = null;

try {

allNetInterfaces = NetworkInterfacegetNetworkInterfaces();

} catch (SocketException e) {

eprintStackTrace();

}

InetAddress ip = null;

NetworkInterface netInterface = null;

while (allNetInterfaceshasMoreElements()) {

netInterface = (NetworkInterface) allNetInterfacesnextElement();

if (netInterfacegetName()trim()equals(eth)){

Enumeration addresses = netInterfacegetInetAddresses();

while (addresseshasMoreElements()) {

ip = (InetAddress) addressesnextElement();

}

break;

}

}

if (ip != null && ip instanceof InetAddress) {

return b = ipgetAddress();

}

return b;

}

补充

// 获取真实IP的方法()

public String getIpAddr(HttpServletRequest request) {

String ip = requestgetHeader(xforwardedfor);

if(ip == null || iplength() == || unknownequalsIgnoreCase(ip)) {

ip = requestgetHeader(ProxyClientIP);

}

if(ip == null || iplength() == || unknownequalsIgnoreCase(ip)) {

ip = requestgetHeader(WLProxyClientIP);

}

if(ip == null || iplength() == || unknownequalsIgnoreCase(ip)) {

ip = requestgetRemoteAddr();

}

return ip;

}

               

上一篇:利用JNative实现Java调用dll动态库

下一篇:JAVA对象转为Java String的几种常用方法