我需要确定给定的IP地址是否来自某个特殊的网络,我必须自动进行身份验证。有谁知道一个java组件来检查IP地址是否来自特定的网络/网络掩码?
18
A
回答
19
Jakarta Commons Net有出现,以满足您的需求org.apache.commons.net.util.SubnetUtils
。它看起来像你做这样的事情:
SubnetInfo subnet = (new SubnetUtils("10.10.10.0", "255.255.255.128")).getInfo();
boolean test = subnet.isInRange("10.10.10.10");
注意,如carson指出,雅加达共享网拥有a bug,防止它在某些情况下给出正确的答案。 Carson建议使用SVN版本来避免此错误。
9
您也可以尝试
boolean inSubnet = (ip & netmask) == (subnet & netmask);
或更短
boolean inSubnet = (ip^subnet) & netmask == 0;
3
使用Spring的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);
}
来源:here
0
来检查IP子网中,我用SubnetUtils类isInRange方法。但是这种方法有一个错误,如果你的子网是X,那么每个低于X的IP地址,isInRange都会返回true。例如,如果您的子网是10.10.30.0/24,并且您想检查10.10.20.5,则此方法返回true。为了处理这个错误,我使用下面的代码。
public static void main(String[] args){
String list = "10.10.20.0/24";
String IP1 = "10.10.20.5";
String IP2 = "10.10.30.5";
SubnetUtils subnet = new SubnetUtils(list);
SubnetUtils.SubnetInfo subnetInfo = subnet.getInfo();
if(MyisInRange(subnetInfo , IP1) == true)
System.out.println("True");
else
System.out.println("False");
if(MyisInRange(subnetInfo , IP2) == true)
System.out.println("True");
else
System.out.println("False");
}
private boolean MyisInRange(SubnetUtils.SubnetInfo info, String Addr)
{
int address = info.asInteger(Addr);
int low = info.asInteger(info.getLowAddress());
int high = info.asInteger(info.getHighAddress());
return low <= address && address <= high;
}
相关问题
- 1. 验证IP地址/网络掩码
- 2. T-Sql:检查IP是否与网络掩码匹配
- 3. 检查IP是否在CIDR网络掩码(范围)
- 4. 使用Python检索具有多个IP地址的接口的网络掩码?
- 5. Golang - 检查是否IP地址是网络中
- 6. 导出网络掩码给定起始和结束IP地址
- 7. 检查IP地址是否在网络中
- 8. 将网络/掩码与另一个网络/掩码比较
- 9. 代码来检查网络是否有或没有为iPhone
- 10. 将CIDR地址转换成子网掩码和网络地址
- 11. 白名单与Azure网络安全组的特定IP地址
- 12. 如何在网络上查找特定桌面的IP地址?
- 13. 从网络上的IP地址查找特定计算机
- 14. 检查流量是否来自特定网址?
- 15. 如何知道从子网掩码搜索哪些IP地址?
- 16. 如何检查IP地址是否在特定的子网内
- 17. 跨IP网络的IP地址
- 18. 是否有任何代码为bitwise和ipv6地址和网络掩码(前缀)?
- 19. 查看网站用户是否来自同一个网络(Intranet网站)
- 20. 如何计算IP地址和网络掩码时的IP范围?
- 21. MAC-OS-X如何让网络细节,如IP地址,子网掩码等
- 22. 如何从Python中的2个IP地址计算网络掩码
- 23. 有一种方法可以知道和检查“谁来自”一个NET/HTTP请求“谁来自”/“来自哪里”?
- 24. 在Ruby上获取特定网络接口的IP地址
- 25. 如何知道网络是否连接?
- 26. 获取网络用户的IP地址
- 27. 网络计算机的IP地址
- 28. 特定MAC地址扫描网络 - Android
- 29. 确定IP地址邮政编码的免费网络服务
- 30. 在Python中检索网络掩码
小心使用此。有一个错误会阻止它正常工作。你可能想把它从SVN中取出来。 http://mail-archives.apache.org/mod_mbox/commons-issues/200902.mbox/%[email protected]%3E – carson 2009-02-23 18:43:01
@carson:感谢您的警告。我编辑了我的答案以包含这些信息。 – Eddie 2009-02-23 20:29:29