[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2013-06-18
4
Medium Priority
?
3,293 Views
Last Modified: 2013-06-19
Hi,
How do I get all the groups in a Active Directory domain using SpringLdap?

Thanks
Jamie
0
Comment
Question by:jamie_lynn
  • 2
4 Comments
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39258308
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
 
LVL 2

Expert Comment

by:titan123
ID: 39259366
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
 

Author Closing Comment

by:jamie_lynn
ID: 39259690
This works great!
Thanks!
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39261485
Not a problem, glad to help!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A solution for Fortify Path Manipulation.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question