Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
listing all functions in JavaScript 19 217
starter POM and spring-boot-starter,  spring-boot-web 2 68
jar file executable 12 58
null output 3 35
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 …
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.
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.

829 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