Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确定和保存远程用户';谁的MAC地址?_Java_Servlets_Mac Address - Fatal编程技术网

Java 如何确定和保存远程用户';谁的MAC地址?

Java 如何确定和保存远程用户';谁的MAC地址?,java,servlets,mac-address,Java,Servlets,Mac Address,我在用密码 byte[] mac = ni.getHardwareAddress(); for (int i = 0; i < mac.length; i++) { System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""); byte[]mac=ni.getHardwareAddress(); for(int i=0;i

我在用密码

byte[] mac = ni.getHardwareAddress(); 
for (int i = 0; i < mac.length; i++) {
    System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
byte[]mac=ni.getHardwareAddress();
for(int i=0;i
输出:
00-27-0E-C2-53-B7

我需要将此输出存储在一个变量中,并需要一个查询将其保存到MySQL数据库。我还希望在登录页面上自动获取MAC地址以及用户详细信息

这样,我就可以将用户的MAC地址及其用户名和密码存储在数据库中。其思想是,当用户登录时,我希望能够自动获取MAC地址以验证用户身份


我该怎么做呢?

你问了很多问题

  • 您的mac地址已存储在变量中。数组mac[]是一个数组变量。如果需要单独的变量,只需按以下方式定义它:

    字符串myMac=mac[i]

  • 在DB中保存数据。我相信您已经在使用DB了。例如,如果您正在使用普通JDBC构造
    insert
    update
    SQL语句,如下所示: 插入UserData('mac')变量(?),其中user_id=? 显然,具体字段取决于您的DB模式。 如果您正在使用某种ORM系统,请询问有关此ORM的更具体问题。但在大多数情况下,这将更加简单。例如,如果您已经拥有类用户:

    类用户{ 私有字符串用户名; 私有字符串密码; //等 }

  • …只需在此处添加新字段
    mac
    : 类用户{ 私有字符串用户名; 私有字符串密码; 私有字符串mac; //等 }

    如果您使用的是JPA,您的DB模式将自动更新,数据也将保存在那里

  • 登录页面也是如此。如果您已经有显示例如用户ID的登录页面,请为MAC添加类似代码
  • 等等等等……

    Python的禅宗说“简单比复杂好。”

    此代码来自SO用户:


    请注意,要运行上述功能,必须有以太网或wifi连接。

    您将无法获取客户端的MAC地址。不,我在询问我的本地MAC地址address@sunny但是,当您无法验证mac地址时,您希望如何使用该地址对用户进行身份验证?请澄清您试图验证的内容do@pekka...存储用户的mac地址以及数据库中的用户名和密码..当他使用用户名和密码登录时,在下一个字段中,我将自动获取mac地址…因此它将验证用户..@pekka…`NetworkInterface ni=NetworkInterface.getByInetAddress(address);if(ni!=null){byte[]mac=ni.getHardwareAddress();`……我忘了提到mac[i]字节[]mac=ni.getHardwareAddress();
    public String obtainMacAddress() throws Exception 
    {
    Process aProc = Runtime.getRuntime().exec("ipconfig /all");
    InputStream procOut = new DataInputStream(aProc.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(procOut));
    
    String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})";
    Pattern aPatternMac = Pattern.compile(aMacAddress);
    String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$";
    Pattern aPatternIp = Pattern.compile(aIpAddress);
    String aNewAdaptor = "[A-Z].*$";
    Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor);
    
    // locate first MAC address that has IP address
    boolean zFoundMac = false;
    boolean zFoundIp = false;
    String foundMac = null;
    String theGoodMac = null;
    
    String strLine;
    while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) {
        Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine);
        if (aMatcherNewAdaptor.matches()) {
            zFoundMac = zFoundIp = false;
        }
        Matcher aMatcherMac = aPatternMac.matcher(strLine);
        if (aMatcherMac.find()) {
            foundMac = aMatcherMac.group(0);
            zFoundMac = true;
        }
        Matcher aMatcherIp = aPatternIp.matcher(strLine);
        if (aMatcherIp.matches()) {
            zFoundIp = true;
            if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac;
        }
    }
    
    aProc.destroy();
    aProc.waitFor();
    
    return theGoodMac;}