2

嗨,我正在使用 Spring ldap,在程序下面执行后它显示我只在这里,之后什么都没有发生,程序处于继续执行模式。

public class SimpleLDAPClient {
        public static void main(String[] args) {
            Hashtable env = new Hashtable();
            System.out.println("I am here");
            String principal = "uid="+"a502455"+", ou=People, o=ao, dc=com";
            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, "MYURL");
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, principal);
            env.put(Context.SECURITY_CREDENTIALS,"PASSWORD");

            DirContext ctx = null;
            NamingEnumeration results = null;
            try {
                ctx = new InitialDirContext(env);

                System.out.println(" Context" + ctx);



                SearchControls controls = new SearchControls();
                controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                results = ctx.search("", "(objectclass=aoPerson)", controls);
                while (results.hasMore()) {
                    SearchResult searchResult = (SearchResult) results.next();
                    Attributes attributes = searchResult.getAttributes();
                    Attribute attr = attributes.get("cn");
                    String cn = (String) attr.get();
                    System.out.println(" Person Common Name = " + cn);
                }
            } catch (NamingException e) {
                throw new RuntimeException(e);
            } finally {
                if (results != null) {
                    try {
                        results.close();
                    } catch (Exception e) {
                    }
                }
                if (ctx != null) {
                    try {
                        ctx.close();
                    } catch (Exception e) {
                    }
                }
            }
        }


    }
4

1 回答 1

1

尝试修复以下行,我删除了“ao”,它工作正常。

results = ctx.search("", "(objectclass=Person)", controls);

您还需要提供搜索基础

env.put(Context.PROVIDER_URL, "ldap://xx:389/DC=test,DC=enterprise,DC=xx,DC=com");

请参阅此链接http://www.adamretter.org.uk/blog/entries/LDAPTest.java

于 2015-06-10T17:12:39.677 回答