2010-06-22 11 views
5

I nostri sviluppatori usano Java su Linux per varie cose (come controllare l'appartenenza ai gruppi ecc.). Funziona - nessun problema! Non sono uno sviluppatore così nudo con me.Autenticazione da Java (Linux) ad Active Directory utilizzando LDAP WITHOUT nomeserver

Il problema è che hanno codificato i nomi dei server dei nostri controller di dominio (server LDAP) nel loro codice. Così ora, quando abbiamo bisogno di sostituirli con DC più recenti, hanno bisogno di cambiare il codice.

Active Directory per natura è ridondante. Il nome di dominio (esempio: domain.local) è un round-robin di tutti i DC: s disponibili per il nostro annuncio.

C'è qualche modo per lo sviluppatore di NON specificare i nomi dei server del controller di dominio ma semplicemente il nome di dominio di Active Directory e quindi il loro server Linux troverà i DC: s disponibili e userà quello che è attivo e funzionante?

Esempi/collegamenti apprezzati. Grazie!

risposta

8

Ovviamente, il nome del server deve essere almeno configurabile, non codificato nell'applicazione.

Tuttavia, dovresti riuscire a trovare il server cercando un record DNS speciale, ovvero un record SRV per _ldap._tcp.DOMAINNAME. I server linux devono essere configurati per utilizzare lo stesso server DNS degli aggiornamenti AD.

per determinare se questo è fattibile, eseguire il comando host -t srv _ldap._tcp.DOMAINNAME sul server linux

Vedi anche Querying the DNS service records to find the hostname and TCP/IP fornisce alcune informazioni su come cercare record SRV in java, e https://community.oracle.com/blogs/kohsuke/2008/06/12/more-active-directory-integration-java

2

Usiamo il codice follow che funziona su una grande quantità di sistemi:

/** 
* 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"); 
} 
Problemi correlati