Skip to content

Instantly share code, notes, and snippets.

@sandipchitale
Last active May 6, 2021 04:41
Show Gist options
  • Save sandipchitale/b0c65b0e44771bacf9035980dcd9adb0 to your computer and use it in GitHub Desktop.
Save sandipchitale/b0c65b0e44771bacf9035980dcd9adb0 to your computer and use it in GitHub Desktop.
Find all groups in LDAP #springboot #ldap
public static class LdapGroupsProvider {
private List<String> groups = new ArrayList<>();
@Override
public List<String> getGroups() {
groups.clear();
final DefaultSpringSecurityContextSource contextSource =
new DefaultSpringSecurityContextSource("ldap://localhost:8389/dc=springframework,dc=org");
contextSource.setAnonymousReadOnly(true);
contextSource.afterPropertiesSet();
LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
try {
ldapTemplate.afterPropertiesSet();
SearchControls controls = new SearchControls();
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "groupOfUniqueNames"));
// List<Group> groups =
List<String> groups = ldapTemplate.search("ou=groups", filter.encode(), controls, new AttributesMapper<String>(){
@Override
public String mapFromAttributes(Attributes attributes) throws NamingException {
Attribute attribute = attributes.get("ou");
return (String) attribute.get() + "@ldap";
}
});
this.groups.addAll(groups);
} catch (Exception e) {
}
return Collections.unmodifiableList(groups);
}
}
@Bean
public LdapGroupsProvider ldapGroupsProvider() {
return new LdapGroupsProvider();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment