无法使用 Spring Ldap 获取 DirContext ctx

发布于 2024-10-18 03:32:54 字数 2006 浏览 2 评论 0原文

您好,我正在使用 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) {
                    }
                }
            }
        }


    }

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


    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

☆獨立☆ 2024-10-25 03:32:54

尝试修复以下几行,我删除了“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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文