国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

封裝JNDI操作LDAP服務器的工具類(4)

2019-11-18 12:24:29
字體:
來源:轉載
供稿:網友

  目標:使用者只需要會使用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;
  }
  }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桑植县| 甘孜县| 丰原市| 凌云县| 广南县| 丰都县| 大方县| 塔城市| 普洱| 永靖县| 庆城县| 维西| 南召县| 鹰潭市| 阜城县| 卓尼县| 桃源县| 博野县| 大竹县| 旬阳县| 绩溪县| 淳化县| 盘山县| 安仁县| 河池市| 隆安县| 遵义市| 天全县| 南漳县| 富源县| 咸丰县| 临桂县| 友谊县| 赣州市| 瓦房店市| 牟定县| 古浪县| 满洲里市| 和硕县| 牡丹江市| 炉霍县|