目标:使用者只需要会使用list,map 数据结构,将对ldap的操作进行封装
类:主要有三个类
1 env类 包含ldap的连接信息
2 ldapconnectionfactory类 ldap连接工厂,提供初始化及获取ldap连接的方法
3 ldapoperutils ldap的处理工具类,提供了各种操作ldap的方法。
接 封装jndi操作ldap服务器的工具类(3) ldapoperutils类的其余方法
/**
* 在当前连接的dircontext 修改指定context下的一个 或 多个属性
* @param context 连接的dircontext
* @param cn 指定context下的名字
* @param attmap 包含list key为属性名称,当属性为多值时
* value 为包含多值的list,为单值时,为包含单值的string类型
* @throws baseexception
* @throws namingexception
*/
public static void modifyattributes(dircontext context, string cn,
map attmap) throws
baseexception, namingexception {
// 参数为空
if (context == null) {
string[] args = {
"context"};
// 打印错误日志
stringbuffer msglog = new stringbuffer(
"empty invoke parameter context null ");
log.error(msglog.tostring());
throw new baseexception("error.common.parameter.empty", args);
}
// 参数为空
if (attmap == null) {
string[] args = {
"attmap"};
// 打印错误日志
stringbuffer msglog = new stringbuffer(
"empty invoke parameter attmap null ");
log.error(msglog.tostring());
throw new baseexception("error.common.parameter.empty", args);
}
// 参数为空
if (stringutils.isempty(cn)) {
string[] args = {
"cn"};
// 打印错误日志
stringbuffer msglog = new stringbuffer(
"empty invoke parameter cn null ");
log.error(msglog.tostring());
throw new baseexception("error.common.parameter.empty", args);
}
// 为空,退出
if (attmap.isempty()) {
return;
}
// 取所有的属性key
set keyset = attmap.keyset();
iterator keyiterator = keyset.iterator();
attributes attrs = new basicattributes();
// 迭代所有的属性key
while (keyiterator.hasnext()) {
// 取下一个属笥
string key = (string) keyiterator.next();
attribute att = null;
object valueobj = attmap.get(key);
if (valueobj instanceof list) {
// 为list ,为多值属性
att = new basicattribute(key);
list valuelist = (list) valueobj;
// 加入多值属性
for (int i = 0; i < valuelist.size(); i++) {
att.add(valuelist.get(i));
}
} else if (valueobj instanceof string) {
att = new basicattribute(key, valueobj);
}
// 加入
attrs.put(att);
}
context.modifyattributes(cn, dircontext.replace_attribute, attrs);
// context.close();
}
//
/**
* 获取连接的dircontext中指定context下的指定属性
* @param context 连接的dircontext
* @param cn 指定context的名称
* @param attnamelist 要取的属性的名称list
* @return map包含list ,key 为属性的名称,当属性值为多值时,value为list类型,
* 否则,value 为string 类型
* @throws namingexception
*/
public static map getattributes(dircontext context, string cn,
list attnamelist) throws namingexception {
map attsmap = new hashmap();
attributes results = null;
list attvallist = null;
string attrid = null;
if (attnamelist == null) {
results = context.getattributes(cn);
} else {
if (!attnamelist.isempty()) {
// results = context.getattributes(cn);
string[] sttemp = new string[attnamelist.size()];
/////////////////////////////////////////// 以下方法性能太低 ////////////////////////////////
// for (int i = 0; i < attnamelist.size(); i++) {
// sttemp[i] = (string) attnamelist.get(i);
// }
// results = context.getattributes(cn,
// sttemp);
///////////////////////////////////////////////////////////////////////////////////////////
// 比较高性能的list 转为 数组的方法
results = context.getattributes(cn,
(string[]) (attnamelist.toarray(sttemp)));
}
}
for (int i = 0; i < attnamelist.size(); i++) {
attribute attr = results.get((string) attnamelist.get(i));
attrid = (string) attnamelist.get(i);
if (attr != null) {
if (attr.size() > 0) {
namingenumeration 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) {
attsmap.put(attrid, attvallist);
// 清空
attvallist = null;
} else {
attsmap.put(attrid, obj1);
}
}
}
}
// context.close();
return attsmap;
}
/**
* 在当前连接的dircontext 获取指定context下的指定属性名称的所有属性值(一个或多个值)
* @param context 连接的dircontext
* @param cn 指定context的cn名
* @param attname 属性名称
* @return 返回包括属性值的list 注意,当属性只有一个值时,返回的list长度为1,当属性
* 是多值属性时,返回list长度为属性值的数目
* @throws namingexception
*/
public static list getattributevalues(dircontext context, string cn,
string attname) throws
namingexception {
list attvallist = new arraylist();
list attnamelist = new arraylist();
attnamelist.add(attname);
map attmap = null;
attmap = getattributes(context, cn, attnamelist);
if (attmap != null) {
object attvalobj = attmap.get(attname);
if (attvalobj instanceof string) {
attvallist.add((string) attvalobj);
} else if (attvalobj instanceof list) {
attvallist = ((list) attvalobj);
}
}
// context.close();
return attvallist;
}
/**
* 获取角色的相关信息
* @param context dircontext
* @param cn string
* @param attname string
* @return string
* @throws namingexception
*/
public static string getroleattributevalues(dircontext context, string cn,
string attname) throws
namingexception {
string result = "";
list attnamelist = new arraylist();
attnamelist.add(attname);
map attmap = null;
attmap = getattributes(context, cn, attnamelist);
if (attmap != null) {
object attvalobj = attmap.get(attname);
result = (string)attvalobj;
}
return result;
}
/**
* 根据条件查找指定cn的context下的一层所有属性
* @param context 连接了的dircontext
* @param cn 要查询的basecn名称
* @param filter 要查询的过滤字符串
* @return 符合查询结果的list
* @throws namingexception
*/
public static list searchcontextone(dircontext context, string cn,
string filter) throws
namingexception {
list resultlist = new arraylist();
map resultrowmap = null;
list attvallist = null;
string attvalstr = null;
// 实例化一个搜索器
searchcontrols constraints = new searchcontrols();
// 设置搜索器的搜索范围
constraints.setsearchscope(searchcontrols.onelevel_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 要查询的basecn名称
* @param filter 要查询的过滤字符串
* @return 符合查询结果的list
* @throws namingexception
*/
public static list searchcontextsub(dircontext context, string cn,
string filter) throws
namingexception {
list resultlist = new arraylist();
map resultrowmap = null;
list attvallist = null;
// 实例化一个搜索器
searchcontrols constraints = new searchcontrols();
// 设置搜索器的搜索范围
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;
}
}
闽公网安备 35060202000074号