【问题标题】:Fetching data from LDAP active directory by using id?使用 id 从 LDAP 活动目录中获取数据?
【发布时间】:2011-11-08 15:11:25
【问题描述】:

我想使用 ID 从 LDAP Active Directory 中获取一些用户信息。 这是我正在尝试连接并获取它的代码。

SearchControls ctls = new SearchControls();
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration results = ctx.search("DC=erieinsurance,DC=com", "(&(objectCategory=user)(name{0}))", 
                    new Object[]{Id}, // filter arguments
                ctls); // search controls
            }
if (results.hasMoreElements()) {

}

它没有返回给定名称和sn的对应值。

上面的过滤器有什么问题吗? 任何建议将不胜感激。

【问题讨论】:

    标签: active-directory jndi ldap-query


    【解决方案1】:

    看起来您正在使用 JNDI。

    这是一个小样本

    //Create the initial directory context
    LdapContext ctx = new InitialLdapContext(env,null);
    
    //Create the search controls
    SearchControls ctls = new SearchControls();
    
    //Specify the attributes to return
    String returnedAtts[]={"distinguishedName","CN","sAMAccountName"};
    ctls.setReturningAttributes(returnedAtts);
    
    //Specify the search scope
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
    //specify the LDAP search filter
    String searchFilter = "(&(objectClass=user)(sAMAccountName="+ theUserToCheck +"))";
    
    //Specify the Base for the search
    String searchBase = "DC=erieinsurance,DC=com";
    
    //initialize counter to total the results
    int totalResults = 0;
    
    //Search for objects using the filter
    NamingEnumeration answer = ctx.search(searchBase, searchFilter, ctls);
    
    //Loop through the search results
    while (answer.hasMoreElements()) {
        SearchResult sr = (SearchResult)answer.next();
        totalResults++;
        System.out.println(totalResults + ". " + sr.getName().toString());
    }
    

    【讨论】:

    • 感谢您的回复...是的,我们正在使用 JNDI。现在已解决,搜索参数 Id 已映射到不同的参数,现在我已映射到“CN”......所以它正在工作......
    • "theUserToCheck" 容易受到 LDAP 过滤器注入的影响,例如如果有人指定了 UserToCheck = "*)(otherAttr=otherValue"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    相关资源
    最近更新 更多