【问题标题】:Authenticating from Java (Linux) to Active Directory using LDAP WITHOUT servername使用没有服务器名的 LDAP 从 Java (Linux) 向 Active Directory 进行身份验证
【发布时间】:2010-06-22 09:50:21
【问题描述】:

我们的开发人员在 Linux 上使用 Java 来处理各种事情(例如检查组的成员资格等)。它有效 - 没问题!

问题是他们在代码中硬编码了我们的域控制器(LDAP 服务器)的服务器名称。所以现在当我们需要用更新的 DC 替换它们时,它们需要更改代码。

Active Directory 本质上是多余的。域名(例如:domain.local)是我们的 AD 可用的所有 DC:s 的循环。

开发人员有什么方法可以不指定域控制器服务器名称,而只指定 Active Directory 域名,然后他们的 Linux 服务器会找到可用的 DC:s 并使用启动并运行的任何一个?

示例/链接赞赏。谢谢!

【问题讨论】:

    标签: java linux active-directory ldap


    【解决方案1】:

    显然,服务器名称至少应该是可配置的,而不是硬编码到应用程序中。

    但是,您应该能够通过查找特殊 DNS 记录(即_ldap._tcp.DOMAINNAME 的 SRV 记录)找到服务器。 linux 服务器必须配置为使用与 AD 更新相同的 DNS 服务器。

    要确定这是否可行,请在您的 linux 服务器上运行命令 host -t srv _ldap._tcp.DOMAINNAME

    另见Querying the DNS service records to find the hostname and TCP/IP 提供了一些关于如何在 java 中查找 SRV 记录的信息,https://community.oracle.com/blogs/kohsuke/2008/06/12/more-active-directory-integration-java

    【讨论】:

      【解决方案2】:

      我们使用适用于大量系统的以下代码:

      /**
       * Detect the default LDAP server
       * @return server:port or null
       */
      String getDefaultLdapHost() {
          try {
              Hashtable<String, String> env = new Hashtable();
              env.put( "java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory" );
              DirContext dns = new InitialDirContext( env );
      
              InetAddress address = InetAddress.getLocalHost();
              String domain = address.getCanonicalHostName();
      
              if( domain.equals( address.getHostAddress() ) ) {
                  //domain is a ip address
                  domain = getDnsPtr( dns );
              }
      
              int idx = domain.indexOf( '.' );
              if( idx < 0 ) {
                  //computer is not in a domain? We will look in the DNS self.
                  domain = getDnsPtr( dns );
                  idx = domain.indexOf( '.' );
                  if( idx < 0 ) {
                      //computer is not in a domain
                      return null;
                  }
              }
              domain = domain.substring( idx + 1 );
      
              Attributes attrs = dns.getAttributes( "_ldap._tcp." + domain, new String[] { "SRV" } );
      
              Attribute attr = attrs.getAll().nextElement();
              String srv = attr.get().toString();
      
              String[] parts = srv.split( " " );
              return parts[3] + ":" + parts[2];
          } catch( Exception ex ) {
              ex.printStackTrace();
              return null;
          }
      }
      
      /**
       * Look for a reverse PTR record on any available ip address
       * @param dns DNS context
       * @return the PTR value
       * @throws Exception if the PTR entry was not found
       */
      private String getDnsPtr( DirContext dns ) throws Exception {
          Exception exception = null;
          Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
          while(interfaces.hasMoreElements()) {
              NetworkInterface nif = interfaces.nextElement();
              if( nif.isLoopback() ) {
                  continue;
              }
              Enumeration<InetAddress> adresses = nif.getInetAddresses();
              while(adresses.hasMoreElements()) {
                  InetAddress address = adresses.nextElement();
                  if( address.isLoopbackAddress() || address instanceof Inet6Address) {
                      continue;
                  }
                  String domain = address.getCanonicalHostName();
                  if( !domain.equals( address.getHostAddress() ) && (domain.indexOf( '.' ) > 0) ) {
                      return domain;
                  }
      
                  String ip = address.getHostAddress();
                  String[] digits = ip.split( "\\." );
                  StringBuilder builder = new StringBuilder();
                  builder.append( digits[3] ).append( '.' );
                  builder.append( digits[2] ).append( '.' );
                  builder.append( digits[1] ).append( '.' );
                  builder.append( digits[0] ).append( ".in-addr.arpa." );
                  try {
                      Attributes attrs = dns.getAttributes( builder.toString(), new String[] { "PTR" } );
                      return attrs.get( "PTR" ).get().toString();
                  } catch( Exception ex ) {
                      exception = ex;
                  }
              }
          }
          if( exception != null ) {
              throw exception;
          }
          throw new IllegalStateException("No network");
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-27
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多