开发者

Not able to get DirContext ctx using Spring Ldap

Hi i am using Spring ldap , after execution below program It display I am here only and after that nothing is happening, program is in continue execution mode.

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开发者_StackOverflow社区 e) {
                    }
                }
                if (ctx != null) {
                    try {
                        ctx.close();
                    } catch (Exception e) {
                    }
                }
            }
        }


    }


Try fixing the below lines, i removed "ao" and it works fine.

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

You need to give search base as well

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

Refer this link as well http://www.adamretter.org.uk/blog/entries/LDAPTest.java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜