AD域的Java开发操作

  1. import java.util.ArrayList;  
  2. import java.util.HashMap;  
  3. import java.util.Hashtable;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6. import javax.naming.Context;  
  7. import javax.naming.NamingEnumeration;  
  8. import javax.naming.directory.Attributes;  
  9. import javax.naming.directory.SearchControls;  
  10. import javax.naming.directory.SearchResult;  
  11. import javax.naming.ldap.InitialLdapContext;  
  12. import javax.naming.ldap.LdapContext;  
  13.   
  14. /** 
  15.  * LDAP 连接 
  16.  */  
  17. public class LDAPConnection {  
  18.   
  19.     private String baseDN;  
  20.     private String filter;  
  21.     private String[] attributes;  
  22.     private Hashtable<String, String> env = null;  
  23.   
  24.     private static LDAPConnection lc;  
  25.   
  26.     private LDAPConnection() {  
  27.   
  28.         //搜索根节点  
  29.         baseDN = "******";  
  30.         //要查询的属性列  
  31.         attributes = new String[]{"cn"};  
  32.         //过滤条件  
  33.         if ((filter == null) || (filter == ""))  
  34.             filter = "objectclass=*";  
  35.     }  
  36.   
  37.     public static LDAPConnection getInstance() {  
  38.         if (lc == null) {  
  39.             lc = new LDAPConnection();  
  40.         }  
  41.         return lc;  
  42.     }  
  43.   
  44.     /** 
  45.      * 建立LDAP连接 
  46.      * @return boolean 
  47.      */  
  48.     private LdapContext getLdapContext() {  
  49.         env = new Hashtable<String, String>();  
  50.         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");  
  51.         env.put(Context.PROVIDER_URL, "ldap://*****:389");  
  52.         env.put(Context.SECURITY_AUTHENTICATION, "simple");  
  53.         env.put(Context.SECURITY_CREDENTIALS, "123456");  
  54.         env.put(Context.SECURITY_PRINCIPAL, "****");  
  55.         try {  
  56.             return new InitialLdapContext(env, null);  
  57.         } catch (Exception e) {  
  58.             System.out.println("连接服务器失败!");  
  59.             e.printStackTrace();  
  60.         }  
  61.         return null;  
  62.     }  
  63.     /**  
  64.      * 获取用户信息  
  65.      * @return List<Map>  
  66.      */  
  67.     public List<Map> getUsers() {  
  68.           
  69.         LdapContext ctx = getLdapContext();  
  70.         if(ctx == null){  
  71.             return null;  
  72.         }  
  73.         List<Map> list = new ArrayList<Map>();  
  74.         try {  
  75.             SearchControls constraints = new SearchControls();  
  76.             constraints.setReturningAttributes(attributes);  
  77.             constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);  
  78.             NamingEnumeration<?> en = ctx.search(baseDN, filter, constraints);  
  79.   
  80.             while (en != null && en.hasMoreElements()) {  
  81.                 Object obj = en.nextElement();  
  82.                 if (obj instanceof SearchResult) {  
  83.   
  84.                     SearchResult si = (SearchResult) obj;  
  85.   
  86.                     Attributes attrs = si.getAttributes();  
  87.                     Map<String, Object> map = new HashMap<String, Object>();  
  88.                     for (int i = 0; i < attributes.length; i++) {  
  89.                         String attributeName = attributes[i];  
  90.   
  91.                         if(attrs.get(attributeName) == null){  
  92.                             map.put(attributeName, attrs.get(attributeName));  
  93.                         }else{  
  94.                             map.put(attributeName, attrs.get(attributeName).get());  
  95.                         }  
  96.                     }  
  97.                     System.out.println(map);  
  98.                     list.add(map);  
  99.                 } else {  
  100.                     System.out.println(obj);  
  101.                 }  
  102.             }  
  103.         } catch (Exception e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.         System.out.println("总符合条件记录数:"+list.size());  
  107.         return list;  
  108.     }  
  109.   
  110.     public static void main(String arg[]) {  
  111.         LDAPConnection.getInstance().getUsers();  
  112.     }  
  113.   
  114. }  

猜你喜欢

转载自alex4java.iteye.com/blog/2296355