JAVA访问ldap

  1. import java.util.Properties;
  2. import javax.naming.*;
  3. import javax.naming.directory.*;
  4. import javax.naming.ldap.*;
  5. public class LDAPTest {
  6.     public static void main(String[] args) {
  7.         Properties env = new Properties();
  8.     
  9.         env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  10.         env.setProperty(Context.SECURITY_AUTHENTICATION, "none");
  11.         env.setProperty(Context.PROVIDER_URL, "LDAP://yourLdapServer:389");
  12.         try {
  13.             LdapContext ctx = new InitialLdapContext(env, null);
  14.             SearchControls searchCtls = new SearchControls();
  15.             searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
  16.             String searchFilter = "objectClass=*";
  17.             
  18.     //Specify the Base for the search
  19.             String searchBase = "ou=People,O=yourDomain";
  20.     //initialize counter to total the group members
  21.             int totalResults = 0;
  22.     //Specify the attributes to return
  23.             String returnedAtts[] = {"mail"};
  24.             searchCtls.setReturningAttributes(returnedAtts);
  25.     //Search for objects using the filter
  26.             NamingEnumeration answer = ctx.search(searchBase, searchFilter,
  27.                                                   searchCtls);
  28.     //Loop through the search results
  29.             while (answer.hasMoreElements()) {
  30.                 SearchResult sr = (SearchResult) answer.next();
  31.                 System.out.println(">>>" + sr.getName());
  32.                 //Print out the groups
  33.                 Attributes attrs = sr.getAttributes();
  34.                 if (attrs != null) {
  35.                     try {
  36.                         for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) {
  37.                             Attribute attr = (Attribute) ae.next();
  38.                             System.out.println("AttributeID: " + attr.getID());
  39.                             for (NamingEnumeration e = attr.getAll(); e.hasMore();
  40.                                     totalResults++) {
  41.                                 System.out.println("Attributes:"+e.next());                            
  42.                             }
  43.                         }
  44.                     } catch (NamingException e) {
  45.                         e.printStackTrace();
  46.                         System.err.println("Problem listing membership: " + e);
  47.                     }
  48.                 }
  49.             }
  50.             System.out.println("Total groups: " + totalResults);
  51.             ctx.close();
  52.         }catch (NamingException e) {
  53.             e.printStackTrace();
  54.             System.err.println("Problem searching directory: " + e);
  55.         }
  56.     }
  57. }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值