有没有人知道一个java组件来检查IP地址是否来自特定的网络/网络掩码?

mic*_*euz 24 java ip networking

我需要确定给定的IP地址是否来自某个特殊网络,我必须自动进行身份验证.

Edd*_*die 28

Jakarta Commons的网络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");
Run Code Online (Sandbox Code Playgroud)

请注意,正如卡森所指出的那样,Jakarta Commons Net有一个错误,阻止它在某些情况下给出正确的答案.Carson建议使用SVN版本来避免这个错误.

  • 小心使用它.有一个错误会阻止它正常工作.您可能希望将其从SVN中删除.http://mail-archives.apache.org/mod_mbox/commons-issues/200902.mbox/%3C2039253761.1233796319684.JavaMail.jira@brutus%3E (4认同)

Omi*_*mid 24

使用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);
}
Run Code Online (Sandbox Code Playgroud)

来源:这里


Pet*_*rey 11

你也可以试试

boolean inSubnet = (ip & netmask) == (subnet & netmask);
Run Code Online (Sandbox Code Playgroud)

或更短

boolean inSubnet = (ip ^ subnet) & netmask == 0;
Run Code Online (Sandbox Code Playgroud)

  • IPv6 是 128 位值(16 字节),因此它们不能存储在单个 long 中 (3认同)

Sea*_*n F 8

开源 IPAddress Java 库将以多态方式为 IPv4 和 IPv6 执行此操作并处理子网。免责声明:我是那个图书馆的项目经理。

示例代码:

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));
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)


Ska*_*out 6

这是一个适用于 IPv4 和 IPv6 的版本,一个带前缀,一个带网络掩码。

/**
 * Check if IP is within an Subnet defined by Network Address and Network Mask
 * @param  ip
 * @param  net
 * @param  mask
 * @return
 */
public static final boolean isIpInSubnet(final String ip, final String net, final int prefix) {
    try {
        final byte[] ipBin   = java.net.InetAddress.getByName(ip  ).getAddress();
        final byte[] netBin  = java.net.InetAddress.getByName(net ).getAddress();
        if(ipBin.length  != netBin.length ) return false;
        int p = prefix;
        int i = 0;
        while(p>=8) { if(ipBin[i] != netBin[i] ) return false; ++i; p-=8; }
        final int m = (65280 >> p) & 255;
        if((ipBin[i] & m) != (netBin[i]&m) ) return false;

        return true;
    } catch(final Throwable t) {
        return false;
    }
}

/**
 * Check if IP is within an Subnet defined by Network Address and Network Mask
 * @param  ip
 * @param  net
 * @param  mask
 * @return
 */
public static final boolean isIpInSubnet(final String ip, final String net, final String mask) {
    try {
        final byte[] ipBin   = java.net.InetAddress.getByName(ip  ).getAddress();
        final byte[] netBin  = java.net.InetAddress.getByName(net ).getAddress();
        final byte[] maskBin = java.net.InetAddress.getByName(mask).getAddress();
        if(ipBin.length  != netBin.length ) return false;
        if(netBin.length != maskBin.length) return false;
        for(int i = 0; i < ipBin.length; ++i) if((ipBin[i] & maskBin[i]) != (netBin[i] & maskBin[i])) return false;
        return true;
    } catch(final Throwable t) {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)