使用UnboundID LDAP sdk,如何获取特定用户所属的所有LDAP组? (我真的很感激一些示例代码)。UnboundID LDAP SDK:获取用户的所有组
3
A
回答
0
下面的函数仅适用于Active Directory,因为它生成成员资格属性memberOf。如果我将找到一个通用LDAP的方式,我将添加它。
Entry userEntry = ldapConnection.getEntry(userDN);
List<Entry> entryList = new ArrayList();
String[] memberValues = userEntry.getAttributeValues("memberOf");
if (memberValues != null) {
DNEntrySource entrySource = new DNEntrySource(ldapConnection, memberValues);
while (true) {
Entry memberEntry = entrySource.nextEntry();
if (memberEntry == null) {
break;
}
entryList.add(memberEntry);
}
}
return entryList;
2
我有同样的问题。然而,迈克尔的解决方案并不适合我,因为它不能递归地工作。
显然,有一个“神奇的” AD查询得到的所有组递归,见 Find Recursive Group Membership (Active Directory) using C#和How do I filter an LDAP query for groups containing a specific user?
虽然我们的AD管理员已使用LDIFDE获取所有组的命令行,我不能得到的查询使用UnboundID。我们的CN里面有空格,UnboundID增加了一个奇怪的'5c' - 但即使没有空格的(技术)用户,我也没有得到任何结果。
无论如何,这里是我的工作(和低效率)源代码(使用谷歌番石榴)。我省略了一些方法和常量,我想你能猜出为恒OBJECT_CLASS
值:-)
/**
* Gets the groups for a user which is identified by the filter.
* @param filter The filter that identifies the user
* @return The groups for the user
*/
private List<String> getGroups(final Filter filter) {
LDAPConnection connection = null;
try {
connection = getConnection();
String userDN = getUserDN(connection, filter);
if (userDN == null) {
return Collections.emptyList(); // No user found
}
Multimap<String, String> groupsByDN = ArrayListMultimap.create();
getGroupsRecursively(connection, userDN, groupsByDN);
Set<String> groups = new HashSet<>(groupsByDN.values());
for (String dn : groupsByDN.keySet()) {
// The user is not a group...
if (!dn.equals(userDN)) {
DN distinguishedName = new DN(dn);
for (RDN rdn : distinguishedName.getRDNs()) {
if (rdn.hasAttribute(CN)) {
groups.add(rdn.getAttributeValues()[0]);
break;
}
}
}
}
return new ArrayList<String>(groups);
} catch (LDAPException e) {
throw new RuntimeException("Can't search roles for " + filter, e);
} finally {
if (connection != null) {
connection.close();
}
}
}
/**
* Since LDAP groups are stored as a tree, this fetches all groups for a user, starting with the user's DN and then
* fetching every group's groups and so on.
* @param connection The LDAP connection
* @param distinguishedName The distinguished name for which groups are searched
* @param groupsByDN Contains a distinguished name as key and its group name as result; keys are only searched once!
* @throws LDAPSearchException if the LDAP search fails
*/
private void getGroupsRecursively(final LDAPConnection connection, final String distinguishedName,
final Multimap<String, String> groupsByDN) throws LDAPSearchException {
if (!groupsByDN.containsKey(distinguishedName)) {
Filter groupFilter = Filter.createANDFilter(Filter.createEqualityFilter(OBJECT_CLASS, GROUP),
Filter.createEqualityFilter(MEMBER, distinguishedName));
SearchResult result = getSearchResult(connection, groupFilter, CN);
List<SearchResultEntry> searchResults = result.getSearchEntries();
for (SearchResultEntry searchResult : searchResults) {
String groupName = searchResult.getAttributeValue(CN);
groupsByDN.put(distinguishedName, groupName);
getGroupsRecursively(connection, searchResult.getDN(), groupsByDN);
}
}
}
相关问题
- 1. unboundid LDAP SDK如何让所有用户和排除部门
- 2. 如何使用UnboundID LDAP SDK
- 3. 使用unboundid LDAP SDK创建LDAP缓存?
- 4. OpenDJ与UnboundId LDAP SDK for Java
- 5. UnboundID LDAP SDK不在推荐
- 6. 所有认证由UnboundId SDK处理?
- 7. Spring LDAP vs UnboundId LDAP
- 8. UnboundID LDAP SDK绑定使用当前用户的Windows凭据
- 9. 与UnboundID LDAP SDK Active Directory中的Java
- 10. Unboundid LDAP域属性
- 11. 从LDAP获取所有用户
- 12. Unboundid LDAP vs Netscape LDAP性能差距
- 13. EJB 3事务传播和UnboundId LDAP SDK
- 14. 如何获取特定用户的所有LDAP组?
- 15. 的Ldap SASL与unboundID
- 16. 使用UNBoundID SDK解锁用户帐户
- 17. UnboundID vs Apache LDAP APIS
- 18. 如何使用目录服务LDAP获取用户所属的所有组?
- 19. 使用unboundid sdk在ApacheDS服务器组中搜索组中的用户
- 20. UnboundID,LDAP JDK迁移
- 21. 获取LDAP用户组的弹簧
- 22. 从特定群组中的所有用户获取用户名
- 23. DirContext:Active Directory Ldap请求:获取具有父组的用户组
- 24. LDAP服务器vs LDAP sdk
- 25. 使用OpenSSO客户端SDK获取所有ActiveDirectory组
- 26. unboundId LDAP限制搜索
- 27. 如何在android中使用UnboundID LDAP?
- 28. 使用unboundid ldap sdk在openldap服务器中添加条目
- 29. 获取所有用户到subOus在OU的Java LDAP
- 30. 使用Net :: LDAP获取所有用户信息
你发现怎么办呢? – Michael 2013-06-25 12:32:11
没有迈克尔。我没有。 – Neo 2013-07-02 14:23:51