Solved

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

Posted on 2013-06-18
4
2,400 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 35

Accepted Solution

by:
mccarl earned 500 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 35

Expert Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
fix34  challenge 9 98
wordsFront challenge 8 70
array220 challenge 8 48
pairs challenge 5 45
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now