这是连接方法: private void LDAP_connect() {
Hashtable env = new Hashtable();
LdapContext ctx ;
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:333/" + "dc=ccc, dc=com");// LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");

try {
ctx = new InitialLdapContext(env, connCtls);
} catch (javax.naming.AuthenticationException e) {
System.out.println("Authentication faild: " + e.toString());
} catch (Exception e) {
System.out.println("Something wrong while authenticating: "
+ e.toString());
}
}
用这个方法连接没有报错,而且能打印连接对象:
ctx=javax.naming.ldap.InitialLdapContext@47364736这样是不是就连接成功了?那要怎样做才能获取到里面的全部用户信息?高分求高手,谢谢啦

解决方案 »

  1.   


    private LdapContext getNewLdapContext() throws Exception {
    Properties env = new Properties();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put("com.sun.jndi.ldap.connect.pool", "true");
    env.put("java.naming.referral", "follow");
    env.put(Context.PROVIDER_URL, "ldap://xxx.xxx.xxx.xxx:xxxx/");
    env.put(Context.SECURITY_PRINCIPAL, "/*account*/");
    env.put(Context.SECURITY_CREDENTIALS, "/*password*/");
    try {
    LdapContext ldapContext = new InitialLdapContext(env, null);
    return ldapContext;
    } catch (Exception e) {
    throw new Exception("LdapContext initialize error", e);
    }
    } public NamingEnumeration<SearchResult> getAllUsersInfo() throws Exception {
    try {
    return ctxSearch("DC=ccc,DC=com", "objectClass=User", null, SearchControls.SUBTREE_SCOPE);
    } catch (Exception e) {
    throw new Exception("get all users info error", e);
    }
    }

    public void printAllUsersInfo() throws Exception {
    NamingEnumeration<SearchResult> infos = getAllUsersInfo();
    while (infos.hasMoreElements()) {
    NamingEnumeration<? extends Attribute> attrs = infos.next().getAttributes().getAll();
    while (attrs.hasMore()) {
    Attribute attr = attrs.next();
    System.out.println(attr.getID() + ":" + attr.get());
    }
    }
    }
      

  2.   

    ctxSearch 方法的代码没有贴出来,能贴出来吗?