How do I get all the groups in a Active Directory domain using SpringLdap?

Hi,
How do I get all the groups in a Active Directory domain using SpringLdap?

Thanks
Jamie
jamie_lynnAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Here is some example code to do what you are after. Obviously you will need to set your AD's domain controller hostname in the URL, a username/password that has access and the Base DN on the domain tree. Also, you should modify the MyAttrMapper class to return whatever information you are after from each group found (here we just get the common name, cn attribute). Note that this example actually gives TWO ways of retrieving the groups, either a directly single query or (if like us you have 1000's of groups and you get "size limit" exceptions) a paged query that retrieves the groups in smaller lots.

package testSpringLdap;

import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;

import org.springframework.ldap.control.PagedResult;
import org.springframework.ldap.control.PagedResultsCookie;
import org.springframework.ldap.control.PagedResultsDirContextProcessor;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;

public class TestSpringLdap {
    
    private static final boolean USE_PAGED_RESULTS = true;
    
    private static LdapTemplate ldapTemplate;
    
    public static void main(String[] args) throws Exception {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl("ldap://DC_HOSTNAME:389");
        contextSource.setUserDn("username");
        contextSource.setPassword("password");
        contextSource.setBase("DC=example,DC=com");
        contextSource.afterPropertiesSet();
        
        ldapTemplate = new LdapTemplate(contextSource);
        
        if (USE_PAGED_RESULTS) {
            
            // Get all groups in many paged results (needed for large numbers of
            // groups)
            PagedResultsCookie cookie = null;
            PagedResult result;
            
            do {
                result = getAllGroups(cookie);
                System.out.println(result.getResultList());
                cookie = result.getCookie();
            } while (result.getCookie() != null);
            
        } else {
            
            // Get all groups in ONE request (may not work for a large number of
            // groups)
            List<?> groups = ldapTemplate.search("OU=Groups", "(objectclass=group)", new MyAttrMapper());
            System.out.println(groups);
        }
    }
    
    public static PagedResult getAllGroups(PagedResultsCookie cookie) {
        PagedResultsDirContextProcessor contextProcessor = new PagedResultsDirContextProcessor(200, cookie);
        SearchControls searchControls = new SearchControls();
        searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        
        List<?> groups = ldapTemplate.search("OU=Groups", "(objectclass=group)", searchControls, new MyAttrMapper(), contextProcessor);
        
        return new PagedResult(groups, contextProcessor.getCookie());
    }
    
    
    public static class MyAttrMapper implements AttributesMapper {
        @Override
        public Object mapFromAttributes(Attributes attributes) throws NamingException {
            return attributes.get("cn").get();
        }
    }
}

Open in new window

0
 
titan123Commented:
I'm not soo sure, Google out the articles for you...

Hope they work out some how the other for you...

http://static.springsource.org/spring-security/site/docs/3.1.x/reference/ldap.html

ActiveDirectoryLdapAuthenticationProvider

Thanks.
0
 
jamie_lynnAuthor Commented:
This works great!
Thanks!
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Not a problem, glad to help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.