java connection ldap

http://blog.csdn.net/zfpigpig/article/details/21176697

 

First of all, you must understand ldap. You can ask Du Niang about ldap. What I want to mention here is the dn (distinguished name), which is the unique mark in layman's terms.

Then you can now go to the next ldapadmin first, and connect to the already configured ldap through ldapadmin. The following is the picture of Microsoft ad.

 

 

For example, the dn of the user administrator is cn=administrator,cn=users,dc=ds-66,dc=com (in simple terms, it is in the form of full path + domain)

Finally, you can code. The connection code is relatively simple, and the query department using fliter is a little more complicated, but it is easy to understand if you look closely, as follows.

 

[java]  view plain copy  
 
  View code snippets on CODE Derive to my code slice
  1. publicstaticvoid main(String[] args) {    
  2.     String url = "ldap://10.1.0.66:389/";  
  3.     String domain = "dc=ds-66,dc=com";  
  4.     String user = "cn=administrator,cn=users";  
  5.     String password = "111111";  
  6.     Hashtable<String, String> env = new Hashtable<String, String>();  
  7.     env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP 工厂  
  8.     env.put(Context.SECURITY_AUTHENTICATION,  "simple" );  // LDAP access security level  
  9.     env.put(Context.PROVIDER_URL, url);  
  10.     env.put(Context.SECURITY_PRINCIPAL, user+","+domain); //  填DN  
  11.     env.put(Context.SECURITY_CREDENTIALS, password); // AD Password  
  12.     env.put("java.naming.ldap.attributes.binary""objectSid objectGUID");  
  13.     LdapContext ldapCtx = null;  
  14.     try {  
  15.         ldapCtx = new InitialLdapContext(env , null);  
  16.         queryGroup(ldapCtx);  
  17.         //queryUser(ldapCtx);         
  18.           
  19.     } catch (NamingException e) {  
  20.         e.printStackTrace ();  
  21.     } finally {  
  22.         if(ldapCtx != null) {  
  23.             try {  
  24.                 ldapCtx.close();  
  25.             } catch (NamingException e) {  
  26.             }  
  27.         }  
  28.     }  
  29. }  
  30.   
  31. privatestaticvoid queryGroup(LdapContext ldapCtx) throws NamingException {    
  32.     SearchControls searchCtls = new SearchControls();  
  33.     searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);  
  34.     String searchFilter = "objectClass=organizationalUnit";  
  35.     String searchBase = "ou=myDeptSubDept,ou=myDept,dc=DS-66,dc=com";  
  36.     String returnedAtts[] = {"distinguishedName""objectGUID""name"};  
  37.     searchCtls.setReturningAttributes(returnedAtts);  
  38.     NamingEnumeration<SearchResult> answer = ldapCtx.search(searchBase, searchFilter, searchCtls);  
  39.     while (answer.hasMoreElements()) {  
  40.         SearchResult sr = answer.next();  
  41.         Attributes Attrs = sr.getAttributes();  
  42.         if (Attrs != null) {  
  43.             NamingEnumeration<?> ne = Attrs.getAll();  
  44.             while(ne.hasMore()) {  
  45.                 Attribute Attr = (Attribute)ne.next();  
  46.                 String name = Attr.getID();  
  47.                 Enumeration<?> values = Attr.getAll();  
  48.                 if (values != null) { // 迭代  
  49.                     while (values.hasMoreElements()) {  
  50.                         String value = "";  
  51.                         if("objectGUID".equals(name)) {  
  52.                             value = UUID.nameUUIDFromBytes((byte[]) values.nextElement()).toString();  
  53.                         } else {  
  54.                             value = (String)values.nextElement();  
  55.                         }  
  56.                         System.out.println(name + " " + value);  
  57.                     }  
  58.                 }  
  59.             }  
  60.             System.out.println("=====================");  
  61.         }  
  62.     }  
  63.       
  64. }  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326971447&siteId=291194637