熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JSF/JAVA根據IP獲取客戶端Mac地址

2013-11-23 19:23:45  來源: Java核心技術 

  需要對用戶的 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;

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26784.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.