Last active
May 6, 2021 04:41
-
-
Save sandipchitale/b0c65b0e44771bacf9035980dcd9adb0 to your computer and use it in GitHub Desktop.
Find all groups in LDAP #springboot #ldap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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