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地址以验证用户身份
我该怎么做呢?你问了很多问题
insert
或update
SQL语句,如下所示:
插入UserData('mac')变量(?),其中user_id=?
显然,具体字段取决于您的DB模式。
如果您正在使用某种ORM系统,请询问有关此ORM的更具体问题。但在大多数情况下,这将更加简单。例如,如果您已经拥有类用户:
类用户{
私有字符串用户名;
私有字符串密码;
//等
}mac
:
类用户{
私有字符串用户名;
私有字符串密码;
私有字符串mac;
//等
}
如果您使用的是JPA,您的DB模式将自动更新,数据也将保存在那里
请注意,要运行上述功能,必须有以太网或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;}