17

java.net.InetAddress默认使用本地机器的默认主机名解析器解析主机名:

主机名到 IP 地址的解析是通过结合使用本地机器配置信息和网络命名服务(例如域名系统 (DNS) 和网络信息服务 (NIS))来完成的。默认情况下,正在使用的特定命名服务是本地计算机配置的一个。对于任何主机名,都会返回其对应的 IP 地址。[来源]

我们如何在不修改本地机器的默认主机名解析器的情况下配置此行为?

例如,无论如何配置java.net.InetAddress以便它通过 OpenDNS (208.67.222.222, 208.67.220.220) 或 Google Public DNS (2001:4860:4860::8888, 2001:4860:4860: :8844)?

或者是显式创建 DNS 数据包请求、通过java.net.DatagramSocketjava.net.Socket将它们发送到服务器并解析响应的唯一解决方案?

4

4 回答 4

14

Java 9 删除了此功能。您将需要使用第三方 DNS 客户端库。

如果您使用的是 Java 8 或更早版本,您可以执行以下操作:

您可以按照本网站sun.net.spi.nameservice.nameservers的说明设置系统属性。

于 2012-07-27T12:39:23.987 回答
10

通过以下接口并允许访问 java.net.*,可以将自己的 DNS 提供程序与 JDK8 和 JDK9 一起使用。新的提供程序通过“INameService.install(new MyNameService());”安装

public interface INameService extends InvocationHandler {
    public static void install(final INameService dns) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException {
        final Class<?> inetAddressClass = InetAddress.class;
        Object neu;
        Field nameServiceField;
        try {
            final Class<?> iface = Class.forName("java.net.InetAddress$NameService");
            nameServiceField = inetAddressClass.getDeclaredField("nameService");
            neu = Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] { iface }, dns);
        } catch(final ClassNotFoundException|NoSuchFieldException e) {
            nameServiceField = inetAddressClass.getDeclaredField("nameServices");
            final Class<?> iface = Class.forName("sun.net.spi.nameservice.NameService");
            neu = Arrays.asList(Proxy.newProxyInstance(iface.getClassLoader(), new Class<?>[] { iface }, dns));
        }
        nameServiceField.setAccessible(true);
        nameServiceField.set(inetAddressClass, neu);
    }

    /**
     * Lookup a host mapping by name. Retrieve the IP addresses associated with a host
     *
     * @param host the specified hostname
     * @return array of IP addresses for the requested host
     * @throws UnknownHostException  if no IP address for the {@code host} could be found
     */
    InetAddress[] lookupAllHostAddr(final String host) throws UnknownHostException;

    /**
     * Lookup the host corresponding to the IP address provided
     *
     * @param addr byte array representing an IP address
     * @return {@code String} representing the host name mapping
     * @throws UnknownHostException
     *             if no host found for the specified IP address
     */
    String getHostByAddr(final byte[] addr) throws UnknownHostException;

    @Override default public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
        switch(method.getName()) {
        case "lookupAllHostAddr": return lookupAllHostAddr((String)args[0]);
        case "getHostByAddr"    : return getHostByAddr    ((byte[])args[0]);
        default                 :
            final StringBuilder o = new StringBuilder();
            o.append(method.getReturnType().getCanonicalName()+" "+method.getName()+"(");
            final Class<?>[] ps = method.getParameterTypes();
            for(int i=0;i<ps.length;++i) {
                if(i>0) o.append(", ");
                o.append(ps[i].getCanonicalName()).append(" p").append(i);
            }
            o.append(")");
            throw new UnsupportedOperationException(o.toString());
        }
    }
}
于 2018-04-24T11:24:32.980 回答
8

对于高达 8 的 Java 版本,这是我编写的代码foo,用于在 Java 中硬编码系统名称 DNS 解析,以便通过测试用例。它的优点是将您的特定条目附加到默认的 Java 运行时 DNS 解析。

我建议不要在生产中运行它。使用了强制反射访问和 Java 运行时非公共实现类!

private static final String FOO_IP = "10.10.8.111";

/** Fake "foo" DNS resolution */
@SuppressWarnings("restriction")
public static class MyHostNameService implements sun.net.spi.nameservice.NameService {
    @Override
    public InetAddress[] lookupAllHostAddr(String paramString) throws UnknownHostException {
        if ("foo".equals(paramString) || "foo.domain.tld".equals(paramString)) {
            final byte[] arrayOfByte = sun.net.util.IPAddressUtil.textToNumericFormatV4(FOO_IP);
            final InetAddress address = InetAddress.getByAddress(paramString, arrayOfByte);
            return new InetAddress[] { address };
        } else {
            throw new UnknownHostException();
        }
    }
    @Override
    public String getHostByAddr(byte[] paramArrayOfByte) throws UnknownHostException {
        throw new UnknownHostException();
    }
}

static {
    // Force to load fake hostname resolution for tests to pass
    try {
        List<sun.net.spi.nameservice.NameService> nameServices =
            (List<sun.net.spi.nameservice.NameService>)
            org.apache.commons.lang3.reflect.FieldUtils.readStaticField(InetAddress.class, "nameServices", true);
        nameServices.add(new MyHostNameService());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

希望这会有所帮助,但同样要小心使用!

于 2017-05-09T12:37:02.510 回答
5

Java 提供了jdk.net.hosts.file用于添加自定义 DNS 记录的新系统参数,如源代码中所述

 * The HostsFileNameService provides host address mapping
 * by reading the entries in a hosts file, which is specified by
 * {@code jdk.net.hosts.file} system property
 *
 * <p>The file format is that which corresponds with the /etc/hosts file
 * IP Address host alias list.
 *
 * <p>When the file lookup is enabled it replaces the default NameService
 * implementation
 *
 * @since 9
 */
private static final class HostsFileNameService implements NameService {

示例:我们可以使用 JVM Option 启动 Java 应用程序

  • -Djdk.net.hosts.file=/path/to/alternative/hosts_file

hosts_file内容可能在哪里:

127.0.0.1    myserver-a.local
10.0.5.10    myserver-b.local
192.168.0.1  myserver-c.local

它将根据hosts_file

于 2021-05-25T07:04:38.103 回答