-1
目標是在給定用戶標識列表的情況下,使用Java中的LDAP從AD(活動目錄)提取不同的屬性。爲此,我有如何確保名稱有效Active directory user?
public Map<String, List<String>> UserDetails(List<String> userSAMs){
DirContext ldapContext = null;
Map<String, List<String>> detailsMap = null;
ldapContext = new LdapProp().getLdapContext();
String returnedAtts[] = {"cn", "givenName", "memberOf"};
String searchFilter = "(&(objectClass=user)(|"; // long OR statement
for (String sam : listSamids){ // building the LDAP query
// HIC SUNT LEONES
searchFilter = "(" + sam + ")";
}
searchFilter = searchFilter + "))";
NamingEnumeration<SearchResult> answer = ldapContext.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) { // Loop over results
SearchResult sr = (SearchResult) answer.next();
Attributes attrs = sr.getAttributes();
String samStr= attrs.get("sAMAccountName").toString();
samStr.replace("sAMAccountName: ", "");
List<String> propOfSam = new ArrayList();
for (String ldapParam : returnedAtts){
String paramStr = attrs.get(ldapParam).toString();
paramStr.replace(ldapParam + ": ", ""); // extract value
propertiesOfSam.add(paramStr);
}
detailsMap.put(samStrng,propertiesOfSam);
};
return detailsMap;
}
爲了簡單起見,我剝去了代碼中的所有錯誤處理。類LdapProp
封裝了AD服務器的所有LDAP連接詳細信息,如PROVIDER_URL
,SECURITY_PRINCIPAL
,SECURITY_CREDENTIALS
和SEARCH_BASE
。
我的問題:如何保證輸入列表的完整性?代碼中的位置標記爲HIC SUNT LEONES
。
允許的字符列表在RFC 1779 RFC 1779和RFC 2253中描述。