0

我正在尝试使用数据包捕获(pcap4j)获取客户端的 MAC 地址。我已经尝试MacAddress通过MacAddress.getByName(IP_ADDRESS)Where the IP_ADDRESSis taken from创建对象HttpServletRequest.getRemoteAddress()

示例代码

private void bar(HttpServletRequest request) {
        InetAddress addr = InetAddress.getByName(request.getRemoteHost());
        MacAddress address = MacAddress.getByName(addr.getHostName());
        // TODO: Do something with MacAddress object to get MAC Address
    }

现在是问题部分:

我一直在我的本地主机上对此进行测试,request.getRemoteHost()并且addr.getHostName()两者都返回了我的 ipv6 环回地址。因此无法创建 MacAddress 对象,stacktrace:

java.lang.IllegalArgumentException: invalid hex string(localhost), not match pattern(\A[0-9a-fA-F][0-9a-fA-F](\Ql\E[0-9a-fA-F][0-9a-fA-F])*\z)
    at org.pcap4j.util.ByteArrays.parseByteArray(ByteArrays.java:914)
    at org.pcap4j.util.MacAddress.getByName(MacAddress.java:64)
    at org.pcap4j.util.MacAddress.getByName(MacAddress.java:55)
    at com.xxxxx.MacController.bar(MacController.java:121)
    at com.xxxxx.MacController.getMac(MacController.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

我如何确保它适用于 ipv4 和 ipv6,因为我不确定客户端的地址方案是什么。我打算在 RESTful Web 服务中使用它。

4

0 回答 0