2011-12-18 38 views
18

Tôi đang sử dụng Weblogic, Ejb3.0. Java 1.6Cách kết nối với Java vào Active Directory

Tôi cần truy cập Active Directory thông qua mã Java. Tôi đã đọc về một số cách (Kerberos, LDAP)

Bất kỳ ai cũng có thể tư vấn cho tôi về cách làm thoải mái? nơi tôi có thể có một số ví dụ mã đầy đủ,

cảm ơn, ray.

+0

Bạn muốn truy cập AD để làm gì? Kerberos thường được giới hạn trong việc xác thực (mặc dù vé Kerberos của AD cũng chứa một số phần mở rộng của riêng chúng, mà bạn có thể thấy khó đọc từ Java). LDAP cũng có thể thực hiện xác thực, nhưng cũng là một thư mục có thêm thông tin về người dùng. Điểm khác biệt chính là bạn có thể sử dụng Kerberos cho SSO. – Bruno

+0

Hãy chính xác hơn những gì bạn chính xác muốn. –

Trả lời

33

Dưới đây là một mã đơn giản mà xác thực và thực hiện một tìm kiếm LDAP usin JNDI trên W2K3:

class TestAD 
{ 
    static DirContext ldapContext; 
    public static void main (String[] args) throws NamingException 
    { 
    try 
    { 
     System.out.println("Début du test Active Directory"); 

     Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11); 
     ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
     //ldapEnv.put(Context.PROVIDER_URL, "ldap://societe.fr:389"); 
     ldapEnv.put(Context.PROVIDER_URL, "ldap://dom.fr:389"); 
     ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple"); 
     //ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=administrateur,cn=users,dc=societe,dc=fr"); 
     ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=jean paul blanc,ou=MonOu,dc=dom,dc=fr"); 
     ldapEnv.put(Context.SECURITY_CREDENTIALS, "pwd"); 
     //ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl"); 
     //ldapEnv.put(Context.SECURITY_PROTOCOL, "simple"); 
     ldapContext = new InitialDirContext(ldapEnv); 

     // Create the search controls   
     SearchControls searchCtls = new SearchControls(); 

     //Specify the attributes to return 
     String returnedAtts[]={"sn","givenName", "samAccountName"}; 
     searchCtls.setReturningAttributes(returnedAtts); 

     //Specify the search scope 
     searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE); 

     //specify the LDAP search filter 
     String searchFilter = "(&(objectClass=user))"; 

     //Specify the Base for the search 
     String searchBase = "dc=dom,dc=fr"; 
     //initialize counter to total the results 
     int totalResults = 0; 

     // Search for objects using the filter 
     NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls); 

     //Loop through the search results 
     while (answer.hasMoreElements()) 
     { 
     SearchResult sr = (SearchResult)answer.next(); 

     totalResults++; 

     System.out.println(">>>" + sr.getName()); 
     Attributes attrs = sr.getAttributes(); 
     System.out.println(">>>>>>" + attrs.get("samAccountName")); 
     } 

     System.out.println("Total results: " + totalResults); 
     ldapContext.close(); 
    } 
    catch (Exception e) 
    { 
     System.out.println(" Search error: " + e); 
     e.printStackTrace(); 
     System.exit(-1); 
    } 
    } 
} 
+0

Cảm ơn bạn rất nhiều vì mã dễ thương và đẹp mắt này, điều này rất đáng ngại so với các nhà phát triển khác –

11
+0

Vậy tôi nên quyết định có sử dụng LDAP hoặc Kerberos không? có thể được thaat các thư mục hoạt động tôi đang cố gắng truy cập không hỗ trợ Kerberos? – rayman

+0

Tôi có chút quen thuộc với Kerberos tbh. Bạn vừa xác thực với AD hay bạn làm nhiều hơn, như đọc/ghi dữ liệu? Nếu thứ hai có thể là LDAP, nếu trước tiên, không thực sự chắc chắn. – clyfe

+1

@rayman: Kerberos là về xác thực và ủy quyền. Nếu bạn chỉ muốn truy cập một số thông tin được lưu trữ trong một thư mục sử dụng LDAP. Câu hỏi của bạn hơi rộng, có thể bạn có thể phác thảo các yêu cầu của mình. – home

Các vấn đề liên quan