简体   繁体   English

使用Java列出ldap中的所有sAMAccountName

[英]List all the sAMAccountName from ldap using java

I want to get the list of all sAMAccountName from ldap, Below is the method which gives me the Ldap attributes of a user by using samAccountName 我想从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");

}

This method accept the samAccountName and returns the ldap attributes for user I want to get the list of all samAccountName, I have searched for this but i didn't get anything relevent, Can anyone please tell how can i get the list of sAMAccountName. 此方法接受samAccountName并返回我想要获取所有samAccountName列表的用户的ldap属性,我已经搜索过此列表,但没有任何相关信息,任何人都可以告诉我如何获取sAMAccountName列表。

I'm not sure what ldapConnection is. 我不确定ldapConnection是什么。 Is it from unboundid ? 是来自unboundid吗?

Either way from the look of the method the third parameter is your LDAP search filter. 无论哪种方法,第三个参数都是您的LDAP搜索过滤器。 You can simply change this filter to be the following: 您可以简单地将此过滤器更改为以下内容:

(objectClass=user)

So the method call would be: 因此,方法调用为:

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

The SearchResult will then contain all users found under CN=XX,DC=XX,DC=XX . SearchResult然后将包含下找到的所有用户CN=XX,DC=XX,DC=XX

If it is from unboundid, then you can add in a 4th parameter to define that you only want the sAMAccountName ldap attribute to be returned for each result. 如果来自unboundid,则可以添加第4个参数以定义仅希望为每个结果返回sAMAccountName ldap属性。 So this would be: 因此,这将是:

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

For more details on LDAP search filters see the following resource: http://docs.oracle.com/cd/E19528-01/819-0997/gdxpo/index.html 有关LDAP搜索过滤器的更多详细信息,请参见以下资源: http : //docs.oracle.com/cd/E19528-01/819-0997/gdxpo/index.html

I am not sure about the above post as i haven't tried it, But after reading the oracle documentation http://docs.oracle.com/cd/E19957-01/816-6402-10/search.htm I modified my search query and it worked, Here is what i have done 我不确定上面的帖子,因为我还没有尝试过,但是在阅读了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");

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM