从UnboundId LDAP搜索结果中删除用户密码

时间:2018-10-25 18:27:38

标签: ldap unboundid-ldap-sdk

使用ApacheDS,我可以执行DefaultDirectoryService#setPasswordHidden以确保进行LDAP查询时,返回的记录从结果集中删除userPassword属性。

如何使用UnboundId(例如,InMemoryDirectoryServer)实现相同的目标?

1 个答案:

答案 0 :(得分:0)

我能够通过创建自己的InMemoryOperationInterceptor实现这一目标:

static class PasswordRemovingOperationInterceptor 
    extends InMemoryOperationInterceptor {

    @Override
    public void processSearchEntry(InMemoryInterceptedSearchEntry entry) {
        if (!entry.getRequest().getAttributeList().contains("userPassword")) {
            if (entry.getSearchEntry().getAttribute("userPassword") != null) {
                Entry old = entry.getSearchEntry();
                Collection<Attribute> attributes = old.getAttributes().stream()
                    .filter(attribute -> 
                        !"userPassword".equals(attribute.getName()))
                    .collect(Collectors.toList());
                Entry withoutPassword = new Entry(old.getDN(), attributes);
                entry.setSearchEntry(withoutPassword);
            }
        }
    }
}

然后将其添加到启动配置中:

InMemoryDirectoryServerConfig config = ...;
config.addInMemoryOperationInterceptor(new PasswordRemovingOperationInterceptor());

但是,还有一种更优雅的方式吗?