Java 检查 IPv4 或 IPv6 地址是否在给定子网中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17025046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java check if IPv4 or IPv6 address is in a given subnet
提问by Jan H
How can I check if an IP address is in a given subnet? I was able to do this by using Apache Commons SubnetUtils (SubnetUtils.SubnetInfo.isInRange) but it does not support IPv6 yet.
如何检查 IP 地址是否在给定的子网中?我可以通过使用 Apache Commons SubnetUtils ( SubnetUtils.SubnetInfo.isInRange)来做到这一点,但它还不支持 IPv6。
回答by Jan H
edazdarevic's CIDRUtilssupports both IPv4 and IPv6. The example does not mention boolean isInRange(String ipAddress), but it is implemented!
edazdarevic 的 CIDRUtils支持 IPv4 和 IPv6。示例中没有提到 boolean isInRange(String ipAddress),但它实现了!
Another option is java-ipv6, but it does not support IPv4 and requires JDK7.
回答by Omid
Use spring-security-web
's IpAddressMatcher. Unlike Apache Commons Net, it supports both ipv4 and ipv6.
使用spring-security-web
的IpAddressMatcher。与 Apache Commons Net 不同,它同时支持 ipv4 和 ipv6。
import org.springframework.security.web.util.matcher.IpAddressMatcher;
...
private void checkIpMatch() {
matches("192.168.2.1", "192.168.2.1"); // true
matches("192.168.2.1", "192.168.2.0/32"); // false
matches("192.168.2.5", "192.168.2.0/24"); // true
matches("92.168.2.1", "fe80:0:0:0:0:0:c0a8:1/120"); // false
matches("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/120"); // true
matches("fe80:0:0:0:0:0:c0a8:11", "fe80:0:0:0:0:0:c0a8:1/128"); // false
matches("fe80:0:0:0:0:0:c0a8:11", "192.168.2.0/32"); // false
}
private boolean matches(String ip, String subnet) {
IpAddressMatcher ipAddressMatcher = new IpAddressMatcher(subnet);
return ipAddressMatcher.matches(ip);
}
Remarks
评论
If you're not willing to include Spring in your project, check out my other answer, here.
如果您不愿意在您的项目中包含 Spring,请在此处查看我的其他答案。
回答by jgonian
commons-ip-mathprovides support for both IPv4 and IPv6 addresses. Here is how you can check if an IP address is in a given subnet:
commons-ip-math提供对 IPv4 和 IPv6 地址的支持。以下是检查 IP 地址是否在给定子网中的方法:
Ipv4Range.parse("192.168.0.0/24").contains(Ipv4.parse("10.0.0.1"))
// false
Ipv6Range.parse("2001:db8::/32").contains(Ipv6.parse("2001:db8::4"))
// true
(disclaimer, I'm one of the maintainers of commons-ip-math)
(免责声明,我是 commons-ip-math 的维护者之一)
回答by Sean F
The IPAddress Java librarysupports both IPv4 and IPv6 in a polymorphic manner and supports subnets, including methods that check for containment of an address or subnet in a containing subnet. The javadoc is available at the link. Disclaimer: I am the project manager of that library.
IPAddress Java 库以多态方式支持 IPv4 和 IPv6,并支持子网,包括检查包含子网中地址或子网是否包含的方法。链接中提供了 javadoc。免责声明:我是那个图书馆的项目经理。
Example code:
示例代码:
contains("10.10.20.0/30", "10.10.20.3");
contains("10.10.20.0/30", "10.10.20.5");
contains("1::/64", "1::1");
contains("1::/64", "2::1");
contains("1::3-4:5-6", "1::4:5");
contains("1-2::/64", "2::");
contains("bla", "foo");
static void contains(String network, String address) {
IPAddressString one = new IPAddressString(network);
IPAddressString two = new IPAddressString(address);
System.out.println(one + " contains " + two + " " + one.contains(two));
}
Output:
输出:
10.10.20.0/30 contains 10.10.20.3 true
10.10.20.0/30 contains 10.10.20.5 false
1::/64 contains 1::1 true
1::/64 contains 2::1 false
1::3-4:5-6 contains 1::4:5 true
1-2::/64 contains 2:: true
bla contains foo false