- /**
- * 在当前连接的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;
- }
- }
封装JNDI操作LDAP服务器的工具类4
最新推荐文章于 2024-08-22 14:32:03 发布