// 設置搜索器的搜索范圍
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 在基目錄中搜索條件為Env.MY_FILTER的所有屬性 注意:這裡返回是的所有的條目集合
NamingEnumeration results
= context.search(cn, filter, constraints);
// 打印條目的識別名(DN)及其所有的屬性名,值
while (results != null && results.hasMore()) {
// 取一個條目
SearchResult si = (SearchResult) results.next();
// 獲取條目的所有屬性集合
Attributes attrs = si.getAttributes();
if (attrs != null) {
String attrId = null;
// 一行數據
resultRowMap = new HashMap();
// 打印所有屬性值
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements(); ) {
// 獲取一個屬性
Attribute attr = (Attribute) ae.next();
attrId = attr.getID();
Enumeration vals = attr.getAll();
if (vals == null) {
continue;
}
Object obj1 = vals.nextElement();
if (obj1 == null) {
continue;
}
// 迭代這個屬性的所有屬性值
while (vals.hasMoreElements()) {
if (attValList == null) {
attValList = new ArrayList();
attValList.add(obj1);
}
attValList.add(vals.nextElement());
}
// 當屬性為單值域時,存為字符串
// 當屬性為多值域時,存為包含多值域的List
if (attValList != null) {
resultRowMap.put(attrId, attValList);
// 清空
attValList = null;
} else {
resultRowMap.put(attrId, obj1);
}
}
}
resultList.add(resultRowMap);
}
return resultList;
}
/**
* 查找指定CN的Context下的子樹下的指定屬性
* @param context DirContext
* @param cn String
* @param filter String
* @param returnedAtts String[] 屬性名字數組
* @return List
* @throws NamingException
*/
public static List searchContextSub(DirContext context, String cn,
String filter, String[] returnedAtts) throws
NamingException {
List resultList = new ArrayList();
String attrId = null;
List attValList = null;
Map resultRowMap = null;
// 實例化一個搜索器
SearchControls constraints = new SearchControls();
// 設置搜索器的搜索范圍
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
// String[] returnedAtts = {"uniquemember"};
constraints.setReturningAttributes(returnedAtts);
// 條目
NamingEnumeration results
= context.search(cn, filter, constraints);
// 迭代所有的條目
while (results != null && results.hasMore()) {
// 取一個條目
SearchResult si = (SearchResult) results.next();
resultRowMap = new HashMap();
// 獲取條目的指定返回的屬性
Attributes attrs = si.getAttributes();
if (attrs != null) {
// 迭代所有屬性值
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements(); ) {
// 獲取一個屬性
Attribute attr = (Attribute) ae.next();
attrId = attr.getID();
Enumeration vals = attr.getAll();
if (vals == null) {
continue;
}
// 迭代這個屬性的所有屬性值
while (vals.hasMoreElements()) {
if (attValList == null) {
attValList = new ArrayList();
}
attValList.add(vals.nextElement());
}
// 當屬性為單值域時,存為字符串
// 當屬性為多值域時,存為包含多值域的List
if (attValList != null) {
resultRowMap.put(attrId, attValList);
// 清空
attValList = null;
}
}
}
resultList.add(resultRowMap);
}
return resultList;
}
/**
* 查找指定CN的Context下的一層指定屬性
* @param context DirContext
* @param cn String
* @param filter String
* @param returnedAtts String[] 屬性名字數組
* @return List
* @throws NamingException
*/
public static List searchContextOne(DirContext context, String cn,
String filter, String[] returnedAtts) throws
NamingException {
List resultList = new ArrayList();
String attrId = null;
List attValList = null;
Map resultRowMap = null;
// 實例化一個搜索器
SearchControls constraints = new SearchControls();
// 設置搜索器的搜索范圍
constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
// String[] returnedAtts = {"uniquemember"};
constraints.setReturningAttributes(returnedAtts);
// 條目
NamingEnumeration results
= context.search(cn, filter, constraints);
// 迭代所有的條目
while (results != null && results.hasMore()) {
// 取一個條目
SearchResult si = (SearchResult) results.next();
resultRowMap = new HashMap();
// 獲取條目的指定返回的屬性
Attributes attrs = si.getAttributes();
if (attrs != null) {
// 迭代所有屬性值
for (NamingEnumeration ae = attrs.getAll();
ae.hasMoreElements(); ) {
// 獲取一個屬性
Attribute attr = (Attribute) ae.next();
attrId = attr.getID();
Enumeration vals = attr.getAll();
if (vals == null) {
continue;
}
Object obj1 = vals.nextElement();
if (obj1 == null) {
continue;
}
// 迭代這個屬性的所有屬性值
while (vals.hasMoreElements()) {
if (attValList == null) {
attValList = new ArrayList();
attValList.add(obj1);
}
attValList.add(vals.nextElement());
}
// 當屬性為單值域時,存為字符串
// 當屬性為多值域時,存為包含多值域的List
if (attValList != null) {
resultRowMap.put(attrId, attValList);
// 清空
attValList = null;
} else {
resultRowMap.put(attrId, obj1);
}
}
}
resultList.add(resultRowMap);
}
return resultList;
}
/**
* 在當前的連接DirContext 刪除 指定Context 下的 一個屬性裡面包含的子屬性
* @param context 連接後的DirContext
* @param cn 指定Context的名稱
* @param attList 包含要刪除的屬性的名稱
* @throws BaseException
* @throws NamingException
*/
public static void deleteInAttributes(DirContext ctx, String userDN,
List attList,String flag) throws NamingException {
if (attList == null || attList.size() == 0) {
return;
} else {
int size = attList.size();
ModificationItem[] mods = new ModificationItem[size];
for (int i = 0; i < size; i++) {
Attribute att = null;
mods[i] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
new BasicAttribute(
flag, (String) attList.get(i)));
}
ctx.modifyAttributes(userDN, mods);
}
}
/**
* 創建一個連接,通過捕捉Exception來確定該用戶是否存在於目標ldap中
* @param configDto ConfigDto
* @param uid String
* @param passWord char[]
* @return boolean
* @throws NamingException
*/
public static boolean authenticate(ConfigDto configDto, String uid, char[] passWord) throws
NamingException {
Hashtable mEnvironment = new Hashtable();
DirContext mContext = null;
//創建連接
mEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,
configDto.getEnvfactory());
mEnvironment.put(Context.PROVIDER_URL, configDto.getEnvurl());
mEnvironment.put(Context.SECURITY_AUTHENTICATION, "simple");
mEnvironment.put(Context.SECURITY_PRINCIPAL,
Constants.LDAP_PEOPLE_ATTRIBUTE_UID + "=" + uid + "," +
configDto.getEnvPeopleLoc());
mEnvironment.put(Context.SECURITY_CREDENTIALS, passWord);
try {
mContext = new InitialDirContext(mEnvironment);
log.debug("user:"+uid+" login!");
return true;
} catch (AuthenticationException ex) {
log.error("user:"+uid+" don't login because of wrong user name or passWord!");
return false;
}
}