如何使用LdapTemplate获取所有LDAP用户

时间:2013-05-20 04:08:31

标签: spring-security spring-ldap

我正在使用spring-security并希望检索所有用户和所有组以存储在参考表中,这样我就可以快速查找用户而无需查阅LDAP目录。我使用以下附加方法创建了一个LdapAuthoritiesPopulator实现镜像DefaultLdapAuthoritiesPopulator

public final Collection<GrantedAuthority> getAllAuthorities() {
    if (groupSearchBase == null) {
        return new HashSet<>();
    }
    Set<GrantedAuthority> authorities = new HashSet<>();
    Set<String> roles = ldapTemplate.searchForSingleAttributeValues(
            groupSearchBase,
            allAuthorityFilter,
            new String[0],
            groupRoleAttribute);
    for (String role : roles) {
        if (convertToUpperCase) {
            role = role.toUpperCase();
        }
        authorities.add(new SimpleGrantedAuthority(rolePrefix + role));
    }
    return authorities;
}

这允许我检索所有群组,allAuthorityFilter是默认为(&(objectClass=group)(objectCategory=group))的属性。

我现在尝试通过使用以下附加方法创建基于LdapUserSearch的自定义FilterBasedLdapUserSearch来与用户实现相同的目标:

public List<String> findAllUsers() {
    SpringSecurityLdapTemplate template
            = new SpringSecurityLdapTemplate(contextSource);
    template.setSearchControls(searchControls);
    List<String> r = template.search(searchBase,
                                     allUsersFilter,
                                     new AttributesMapper() {
        @Override
        public Object mapFromAttributes(Attributes atrbts)
                throws NamingException {
            return (String) atrbts.get(userNameAttribute).get();
        }
    });
    return r;
}

我有两个问题:

  1. 如果用户列表很大,我会得到javax.naming.SizeLimitExceededException我不知道如何解决。
  2. 我希望此方法返回DirContextOperations类似于searchForUser(String)的方式,以便可以重用我的LdapUserDetailsMapper实现来返回所有用户属性。
  3. 我发现LdapTemplate的文档有点毛茸茸,无法找到我想要的答案,我们将非常感谢您的帮助。

    更新:我已经通过

    解决了上面的第(2)点
    public List<UserDetails> getAllUserDetails(boolean includeAuthorities) {
        List<UserDetails> r = new ArrayList<>();
        for (DirContextOperations ctx : userSearch.findAllUserOperations()) {
            try {
                Attribute att = ctx.getAttributes().get(usernameAttribute);
                String username = (String) att.get();
                r.add(userMapper.mapUserFromContext(
                        ctx,
                        username,
                        includeAuthorities
                            ? authPop.getGrantedAuthorities(ctx, username)
                            : Collections.<GrantedAuthority>emptySet()));
            } catch (NamingException ex) {
                LOG.warn("Username attribute " + usernameAttribute + " not found!");
            }
        }
        return r;
    }
    

    UserSearch实施中,我有:

    public List<DirContextOperations> findAllUserOperations() {
        SpringSecurityLdapTemplate template = new SpringSecurityLdapTemplate(contextSource);
        template.setSearchControls(searchControls);
        return template.search(searchBase,
                               allUsersFilter, new ContextMapper() {
            @Override
            public Object mapFromContext(Object o) {
                return (DirContextOperations) o;
            }
        });
    }
    

    但是我还没有解决第一点。如果我需要以某种方式批量处理,那么只要有一种方法可以告诉LdapTemplate在后​​续调用中恢复的位置,那就没问题了。

0 个答案:

没有答案