使用 Java 获取 LDAP 域用户名列表

发布于 2024-12-21 00:40:32 字数 1904 浏览 1 评论 0原文

LDAP 用户名需要作为自动完成功能显示在输入框中。我正在尝试获取用户列表,如下所示:

        String ldapURL = "ldap://192.26.75.5:389/dc=northamerica,dc=company,dc=com";
    String principalPrefix = "domainName";      
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();

    Hashtable<String, String>environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL,ldapURL);
    environment.put(Context.SECURITY_AUTHENTICATION,"simple");
    environment.put(Context.SECURITY_PRINCIPAL,principalPrefix + "\\" + username);
    environment.put(Context.SECURITY_CREDENTIALS,password);
    environment.put( Context.REFERRAL, "follow" );

    DirContext context = null;
    NamingEnumeration<SearchResult> enumResult = null;      
    try
    {
                    context = new InitialDirContext(environment);                       
                    SearchControls controls = new SearchControls();                     
                    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    String[] attrIDs ={"ou","uid", "givenname", "sn", "mail"};
                    controls.setReturningAttributes(attrIDs);
                    enumResult = context.search("","(&(objectCategory=person)(objectClass=user)(CN=*))", controls);                     
                    if(enumResult != null)
                    {
                                    //authentication successful                                 
                    }                       
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }

但是“enumResult”始终获取单个用户值。如果我遗漏了什么或者这样做的方法是否错误,请告诉我。 任何帮助/建议/建议将不胜感激!谢谢。

The ldap user names need to be displayed in the the input box as autocomplete feature. I am trying to get list of users as below:

        String ldapURL = "ldap://192.26.75.5:389/dc=northamerica,dc=company,dc=com";
    String principalPrefix = "domainName";      
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    String password = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();

    Hashtable<String, String>environment = new Hashtable<String, String>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL,ldapURL);
    environment.put(Context.SECURITY_AUTHENTICATION,"simple");
    environment.put(Context.SECURITY_PRINCIPAL,principalPrefix + "\\" + username);
    environment.put(Context.SECURITY_CREDENTIALS,password);
    environment.put( Context.REFERRAL, "follow" );

    DirContext context = null;
    NamingEnumeration<SearchResult> enumResult = null;      
    try
    {
                    context = new InitialDirContext(environment);                       
                    SearchControls controls = new SearchControls();                     
                    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                    String[] attrIDs ={"ou","uid", "givenname", "sn", "mail"};
                    controls.setReturningAttributes(attrIDs);
                    enumResult = context.search("","(&(objectCategory=person)(objectClass=user)(CN=*))", controls);                     
                    if(enumResult != null)
                    {
                                    //authentication successful                                 
                    }                       
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }

However "enumResult" always gets single user value. Let me know if i am missing out something or if its the wrong way to do it.
Any help/advice/suggestion would be appreciated !! Thanks.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文