【问题标题】:List all the sAMAccountName from ldap using java使用 java 列出来自 ldap 的所有 sAMAccountName
【发布时间】:2014-10-08 10:40:03
【问题描述】:

我想从 ldap 获取所有 sAMAccountName 的列表,下面是使用 samAccountName 为我提供用户的 Ldap 属性的方法

public static void searchUserFromLdap(String samAccountName) throws Exception{

    SearchResult searchResult = ldapConnection.search("CN=XX,DC=XX,DC=XX", SearchScope.SUB, "(sAMAccountName=" + samAccountName +")"); 

    if(searchResult.getSearchEntries().size()<=0){
        System.out.println("No such user found in LDAP");
        return;
    }

    System.out.println("Start :- LDAP attributes for given user\n");
    for(SearchResultEntry searchResultEntry : searchResult.getSearchEntries()){

        System.out.println(searchResultEntry.toLDIFString());
    }

    System.out.println("\nEnd :- LDAP attributes for given user");

}

此方法接受 samAccountName 并返回用户的 ldap 属性 我想获取所有 samAccountName 的列表,我已经搜索过这个但我没有得到任何相关的信息,谁能告诉我如何获取 sAMAccountName 的列表。

【问题讨论】:

    标签: java active-directory ldap


    【解决方案1】:

    我不确定ldapConnection 是什么。是来自unboundid 吗?

    无论从方法的外观来看,第三个参数都是您的 LDAP 搜索过滤器。您可以简单地将此过滤器更改为以下内容:

    (objectClass=user)
    

    所以方法调用应该是:

    SearchResult searchResult = ldapConnection.search(
            "CN=XX,DC=XX,DC=XX", 
            SearchScope.SUB, 
            "(objectClass=user)");
    

    SearchResult 将包含在CN=XX,DC=XX,DC=XX 下找到的所有用户。

    如果它来自 unboundid,那么您可以添加第 4 个参数来定义您只希望为每个结果返回 sAMAccountName ldap 属性。所以这将是:

    SearchResult searchResult = ldapConnection.search(
            "CN=XX,DC=XX,DC=XX", 
            SearchScope.SUB, 
            "(objectClass=user)",
            "sAMAccountName");
    

    有关 LDAP 搜索过滤器的更多详细信息,请参阅以下资源: http://docs.oracle.com/cd/E19528-01/819-0997/gdxpo/index.html

    【讨论】:

    • 你对我的回答是对的,我忘了过滤对象类。很好的答案。
    • 你最好使用objectCategory rather than objectClass
    【解决方案2】:

    我不确定上面的帖子,因为我没有尝试过,但是在阅读了 oracle 文档http://docs.oracle.com/cd/E19957-01/816-6402-10/search.htm 之后,我修改了我的搜索查询并且它有效,这就是我所做的

    public static void getListOfAllSamAccountName() throws Exception {
        List<String> samAccountNameList = null;
        SearchResult searchResult = ldapConnection.search(
                "CN=XX,DC=XX,DC=xx", SearchScope.SUB,
                "(sAMAccountName=*)");
    
        if (searchResult.getSearchEntries().size() <= 0) {
            System.out.println("No such user found in LDAP");
            return;
        }
        samAccountNameList = new ArrayList<String>();
        System.out.println("Start :- LDAP attributes for given user\n");
        for (SearchResultEntry searchResultEntry : searchResult
                .getSearchEntries()) {
            Attribute attribute = searchResultEntry
                    .getAttribute("sAMAccountName");
            String samAccountName = attribute.getValue();
    
            samAccountNameList.add(samAccountName);
    
        }
    
        if (samAccountNameList != null) {
            System.out
                    .println("*******************************List of Same account Name******************************");
            for (String samAccountName : samAccountNameList) {
    
                System.out.println(samAccountName);
            }
        }
    
        System.out.println("\nEnd :- LDAP attributes for given user");
    
    }
    

    【讨论】:

    • 我有相关的问题,虽然我是初学者。 ....如果有子域(childa,abc.com childb.abc.com,childc.abc.com)并且包含相同的samaccountname(123456789)AD将如何进行身份验证?是否会一一检查子域以便对用户进行身份验证?我的应用程序仅使用 samaccountname 作为登录 ID。每个子域中这个 samaccount 的密码是什么不同... ??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多