随笔-86  评论-33  文章-0  trackbacks-0

 

import java.util.Properties;
import javax.naming.*;
import javax.naming.ldap.*;
import javax.naming.directory.*;
public class Client {
public static void main (String[] args) {
    Properties env 
= new Properties();
    String adminName 
= "usish\\derek";
    String adminPassword 
= "dada";
    String ldapURL 
= "LDAP://192.168.40.42:389";
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
"com.sun.jndi.ldap.LdapCtxFactory");
//set security credentials, note using simple cleartext authentication
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, adminName);
    env.put(Context.SECURITY_CREDENTIALS, adminPassword);
//connect to my domain controller
    env.put(Context.PROVIDER_URL, ldapURL);

    
try {
//Create the initial directory context
        LdapContext ctx = new InitialLdapContext(env, null);

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

//Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//specify the LDAP search filter
        String searchFilter = "(&(objectCategory=person)(objectClass=user)(name=004*))";

//Specify the Base for the search
        String searchBase = "OU=USISH,DC=usish,DC=com,DC=cn";
//initialize counter to total the group members
        int totalResults = 0;
//Specify the attributes to return
        String returnedAtts[] = {"memberOf"};
        searchCtls.setReturningAttributes(returnedAtts);

//Search for objects using the filter
        NamingEnumeration answer = ctx.search(searchBase, searchFilter,
                                              searchCtls);
//Loop through the search results
        while (answer.hasMoreElements()) {
            SearchResult sr 
= (SearchResult) answer.next();
            System.out.println(
">>>" + sr.getName());
            
//Print out the groups
            Attributes attrs = sr.getAttributes();
            
if (attrs != null) {
                
try {
                    
for (NamingEnumeration ae = attrs.getAll(); ae.hasMore(); ) {
                        Attribute attr 
= (Attribute) ae.next();
                        System.out.println(
"AttributeID: " + attr.getID());
                        
for (NamingEnumeration e = attr.getAll(); e.hasMore();
                                totalResults
++) {
                            System.out.println(
"Attributes:"+e.next());                            
                        }
                    }
                } 
catch (NamingException e) {
                    e.printStackTrace();
                    System.err.println(
"Problem listing membership: " + e);
                }
            }
        }
        System.out.println(
"Total groups: " + totalResults);
        ctx.close();
    }
catch (NamingException e) {
        e.printStackTrace();
        System.err.println(
"Problem searching directory: " + e);
    }
 }
}
posted on 2007-05-09 15:12 Derek.Guo 阅读(2288) 评论(1)  编辑  收藏 所属分类: Java

评论:
# re: JAVA访问LDAP[未登录] 2008-09-28 13:22 | ice
我在运上面的代码出现了 cuold not find the main class.Program will exit! 问题怎么解决啊 ,能帮帮我吗,我是第一遇到这个问题

  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
MSN:envoydada@hotmail.com QQ:34935442