package test;
import java.util.List;
import com.unboundid.ldap.sdk.LDAPConnection;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.LDAPSearchException;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;
public class Test2 {
public static void main(String[] args) {
try {
List<SearchResultEntry> usersFromLdap = getUsersFromLdap();
System.out.println(usersFromLdap);
} catch (LDAPException e) {
//TODO handle exception
}
}
public static List<SearchResultEntry> getUsersFromLdap() throws LDAPException, LDAPSearchException{
String searchBaseDN = "dc=your-domain,dc=com"; //your-domain.com
String searchFilter = "(&(objectClass=user)(sn=*))"; //see e.g. https://confluence.atlassian.com/kb/how-to-write-ldap-search-filters-792496933.html
LDAPConnection connection = new LDAPConnection("host.your-domain.com", 389);
try {
connection.bind("yourLdapUser", "YourLdapPassword");
SearchRequest request = new SearchRequest(searchBaseDN, SearchScope.SUB, searchFilter);
request.setSizeLimit(0);
SearchResult searchResult = connection.search(request);
List<SearchResultEntry> result = searchResult.getSearchEntries();
return result;
} finally {
if (connection != null) {
connection.close();
}
}
}
}