java连接ldap

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

 

首先必须了解ldap,ldap相关可以问度娘。这边我重点要提的是dn(distinguished name),通俗的来说就是唯一标示。

然后现在可以先下一个ldapadmin,通过ldapadmin先连接已经配置好的ldap,下面是微软ad的图。

 

 

比如administrator这个用户的dn就是cn=administrator,cn=users,dc=ds-66,dc=com(简单来说就是全路径+域的形式)

最后就可以编码了,连接的代码比较简单,使用fliter的查询部门稍微复杂点,不过仔细看看也很容易理解,如下。

 

[java]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. public static void 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访问安全级别  
  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. private static void 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. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值