What library can I use to check if an IP address is in a given subnet? I could find libraries like the Apache Commons SubnetUtils (SubnetUtils.SubnetInfo.isInRange) but many do not support IPv6 yet.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
The IPAddress Java library supports both IPv4 and IPv6 in a polymorphic manner and supports subnets, including methods that check for containment of an address or subnet in a subnet. The javadoc is available at the link.
Here is sample code that solves your problem, which works identically with either IPv4 or IPv6.
edazdarevic's CIDRUtils supports both IPv4 and IPv6. The example does not mention boolean isInRange(String ipAddress), but it is implemented!
Another option is java-ipv6, but it does not support IPv4 and requires JDK7.
commons-ip-math provides support for both IPv4 and IPv6 addresses. Here is how you can check if an IP address is in a given subnet:
(disclaimer, I'm one of the maintainers of commons-ip-math)
Use Spring's IpAddressMatcher. Unlike Apache Commons Net, it supports both ipv4 and ipv6.