指定されたグループから開始し、指定されたグループとサブグループからすべてのユーザーを検索する、Spring LDAP で再帰的な LDAP 検索を実行するプレーンな Java コマンドライン ソフトウェアに取り組んでいます。
グループ識別名に組織単位 (=ou) が含まれている場合、検索は失敗しますが、それ以外の場合は機能します。
以下は実装の短いバージョンで、再帰は省略されています。
private void searchLdapGroup(List<UserDTO> users, LdapTemplate ldapTemplate, String groupName) {
// recursion guard omitted
String base = groupName.substring(groupName.indexOf(',') + 1);
AndFilter filter = new AndFilter().and(new EqualsFilter("objectclass", "group")).and(new EqualsFilter("memberof", groupName));
List<String> subgroups = ldapTemplate.search(base, filter.encode(), new GroupNameMapper());
// recursive calls for subgroups omitted
getAllUsers(users, ldapTemplate, groupName, base);
}
private void getAllUsers(List<UserDTO> users, LdapTemplate ldapTemplate, String groupName, String base) {
AndFilter filter = new AndFilter().and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("memberof", groupName));
// Paged search omitted.
List<UserDTO> result = ldapTemplate.search(base,filter.encode(),new UserAttributesMapper());
users.addAll(result);
}
は、GroupNameMapper
識別名を文字列として返し、やUserAttributesMapper
などのさまざまな属性からユーザー オブジェクトを返します。sAMAccountName
givenName
コード (再帰を使用) は、指定されたグループが次のような最初のテスト グループで 36 人のユーザーすべてを検索します。
CN=import_users,CN=ユーザー,DC=例,DC=テスト,DC=組織
まったく同じテスト環境で、グループ識別名に次のような 1 つ以上の組織単位が含まれている場合、ゼロの人物とサブグループが返されます。
CN=import_users、OU=testou、DC=例、DC=テスト、DC=org
ルックアップをテストしたので、これは間違ったグループ識別名、「memberof」が機能していない、またはグループにユーザーが含まれていないことが原因である可能性はありません。
String[] test = (String[])ldapTemplate.lookup("CN=import_users,OU=testou,DC=example,DC=test,DC=org", new ContextMapper() {
public Object mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
return adapter.getStringAttributes("Member");
}
});
見つける
CN=John Doe,CN=ユーザー,DC=例,DC=テスト,DC=組織
ユーザーJohn Doeのルックアップ
String[] test = (String[])ldapTemplate.lookup("CN=John Doe,CN=Users,DC=example,DC=test,DC=org", new ContextMapper() {
public Object mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
return adapter.getStringAttributes("memberof");
}
});
結果が得られます:
CN=import_users,OU=testou,DC=example,DC=test,DC=org CN=import_users,CN=Users,DC=example,DC=test,DC=org
組織単位が関係している場合、検索で何も見つからないのはなぜですか?
ライブラリ ユーザー: spring-ldap-core - 2.0.4.RELEASE