Q:

Get all groups and roles for current user from LDAP

I'm trying to get all groups and roles for current user from LDAP.
Here is my App.
If user logged in I need to get its roles and groups
I searched many post, but didnt found the answer
@Override
public void run(String... args) throws Exception {

    String searchbase = "dc=springframework,dc=org";

    Hashtable<String, String> environment = new Hashtable<>();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    environment.put(Context.PROVIDER_URL, "ldap://localhost:8389");
    environment.put(Context.SECURITY_AUTHENTICATION, "simple");
    environment.put(Context.SECURITY_PRINCIPAL, "uid=bob,ou=people,dc=springframework,dc=org");
    environment.put(Context.SECURITY_CREDENTIALS, "bobspassword");

    // trying to connect LDAP server
    LdapContext ctx = new InitialLdapContext(environment, null);
    System.out.println("Connected!");

    SearchControls controls = new SearchControls();
    String[] attributes = {"cn","ou"}; 
    controls.setReturningAttributes(attributes); 
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
    NamingEnumeration<?> answer = ctx.search(searchbase, "(&(objectclass=groupOfUniqueNames))", controls); 

    while(answer.hasMore()) { 
        SearchResult rslt = (SearchResult) answer.next(); 
        Attributes attrs = rslt.getAttributes(); 
        String groupsCN = attrs.get("cn").toString(); 
        String groupsOU = attrs.get("ou").toString();

        String [] groupname = groupsCN.split(":"); 
        String userGroup = groupname[1]; 
        System.out.println(attrs.get("cn")); 

        String [] groupnameOU = groupsOU.split(":"); 
        String userGroupOU = groupnameOU[1]; 
        System.out.println(attrs.get("ou"));
    } 
    System.out.println("Listed!");
    ctx.close();
    System.exit(1);             
}

and My LDAP server. It is a embedded LDAP server in Spring Boot
dn: dc=springframework,dc=org
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: springframework

dn: ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: groups

dn: ou=subgroups,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: subgroups

dn: ou=people,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=ben,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Ben Alex
sn: Alex
uid: ben
userPassword: password

dn: uid=bob,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Bob Hamilton
sn: Hamilton
uid: bob
userPassword: bobspassword

dn: cn=developers,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: developers
ou: developer
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org
uniqueMember: uid=bob,ou=people,dc=springframework,dc=org

dn: cn=managers,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: managers
ou: manager
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org
uniqueMember: uid=bob,ou=people,dc=springframework,dc=org

dn: cn=submanagers,ou=subgroups,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfUniqueNames
cn: submanagers
ou: submanager
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

I need to get roles and groups for user. Now I get all groups and roles. Somthing wrong in filter?

A:

You filter "(&(objectclass=groupOfUniqueNames))" is just requesting all groups.
If you need the groups for a user (Ben), you want to filter the groups when the user is a member: "(&(objectclass=groupOfUniqueNames)(uniqueMember=uid=ben,ou=people,dc=springframework,dc=org))" 

