code: public List<String> getGroups(String username, String property) {
          final List<String> result = new ArrayList<>();
          ldapTemplate.search(query().where("cn").is(username), new AttributesMapper<String>() {
              public String mapFromAttributes(Attributes attrs) throws NamingException {
                  NamingEnumeration<?> ous = attrs.get(property).getAll();
                  // since we can generate multiple values here but may only return a single string, we ignore
                  // the ldapTemplate#search result and just put the values in our own list, returning an empty string
                  // which is effectively ignored
                  while (ous.hasMore()) {
                      result.add((String) ous.next());
                  }
                  return "";
              }
          });
          return result;
      }
docs: Extract groups from ldap.
  
  @param username username to search for
  @param property the property to extract the groups from
  @return a list of group names from ldap