Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stringclean challenge 26 74
countHi2 challenge 7 56
bitbucket vs gitbucket 3 82
java 8 lambda expresssions exception handling 3 91
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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.

832 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