使用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