我想从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并返回user的ldap属性 我想得到所有samAccountName的列表,我已经搜索过这个,但我没有得到任何相关内容,任何人都可以告诉我如何获得sAMAccountName列表。
答案 0 :(得分: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
答案 1 :(得分:0)
我不确定上面的帖子,因为我没有尝试过,但是在阅读了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");
}