使用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. Spring LDAP vs UnboundId LDAP
- 5. 与UnboundID LDAP SDK Active Directory中的Java
- 6. Unboundid LDAP域属性
- 7. 从LDAP获取所有用户
- 8. EJB 3事务传播和UnboundId LDAP SDK
- 9. 的Ldap SASL与unboundID
- 10. 使用unboundid sdk在ApacheDS服务器组中搜索组中的用户
- 11. 从特定群组中的所有用户获取用户名
- 12. unboundId LDAP限制搜索
- 13. 使用OpenSSO客户端SDK获取所有ActiveDirectory组
- 14. 使用unboundid ldap sdk在openldap服务器中添加条目
- 15. 获取所有用户到subOus在OU的Java LDAP
- 16. 使用Net :: LDAP获取所有用户信息
- 17. UnboundID LDAP SDK的认证步骤是什么
- 18. 获取Active Directory中的所有用户
- 19. Rhino Security,获取所有用户组
- 20. 从TDS LDAP获取所有用户,包括图像
- 21. PHP LDAP获取用户SID
- 22. 使用unboundid在LDAP中更改用户密码
- 23. LDAP获取组从
- 24. LDAP:如何获取Active Directory中的所有组?
- 25. 获取Active Directory组中的所有用户
- 26. 如何使用Java中的UnboundID LDAP SDK从Active Directory中解析objectGUID?
- 27. 如何获得所有使用ldap的用户?
- 28. 如何获取本地用户所属的所有本地组
- 29. LDAP获取完整的用户名C++
- 30. 活动目录:获取用户所在的组
你发现怎么办呢? – Michael 2013-06-25 12:32:11
没有迈克尔。我没有。 – Neo 2013-07-02 14:23:51