2010-06-22 4 views
5

हमारे डेवलपर्स विभिन्न चीजों के लिए लिनक्स पर जावा का उपयोग करते हैं (जैसे समूहों की सदस्यता जांचना आदि)। यह काम करता है - उसमें कोई समस्या नहीं है! मैं एक डेवलपर नहीं हूं जो मेरे साथ इतना बेकार है।जावा (लिनक्स) से सक्रिय निर्देशिका में प्रमाणीकरण से एलडीएपी का उपयोग कर सीरनावैम

समस्या यह है कि वे अपने कोड में हमारे डोमेन नियंत्रकों (LDAP सर्वर) के servernames hardcoded किया है। तो अब जब हमें उन्हें नए डीसी के साथ बदलने की जरूरत है, तो उन्हें कोड बदलने की जरूरत है।

प्रकृति द्वारा सक्रिय निर्देशिका अनावश्यक है। डोमेन नाम (उदाहरण: domain.local) हमारे डीडी के लिए उपलब्ध सभी डीसी: के राउंड-रॉबिन है।

डेवलपर के लिए डोमेन नियंत्रक सर्वर नाम निर्दिष्ट नहीं करने का कोई तरीका है लेकिन केवल सक्रिय निर्देशिका डोमेन नाम और फिर उनके लिनक्स सर्वर को डीसी: उपलब्ध होगा और जो भी हो रहा है उसका उपयोग करेगा और चल रहा है?

उदाहरण/लिंक की सराहना की। धन्यवाद!

उत्तर

8

जाहिर है, सर्वर नाम कम से कम विन्यास योग्य होना चाहिए, आवेदन में हार्ड कोड नहीं होना चाहिए।

हालांकि, अगर आप एक विशेष DNS रिकॉर्ड, _ldap._tcp.DOMAINNAME के लिए अर्थात् एक SRV रिकॉर्ड ऊपर देखकर भी सर्वर को खोजने के लिए सक्षम होना चाहिए। लिनक्स सर्वर को आपके एडी अपडेट के समान DNS सर्वर का उपयोग करने के लिए कॉन्फ़िगर किया जाना है।

निर्धारित करने के लिए कि क्या यह संभव है, अपने लिनक्स सर्वर

भी देखें Querying the DNS service records to find the hostname and TCP/IP जावा में SRV रिकॉर्ड को देखने के लिए कुछ सहायता की जानकारी प्रदान करता है पर आदेश host -t srv _ldap._tcp.DOMAINNAME चलाते हैं, और 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"); 
} 

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^