封装JNDI操作LDAP服务器的工具类4

  1. /**   
  2.      * 在当前连接的DirContext 修改指定Context下的一个 或 多个属性   
  3.      * @param context 连接的DirContext   
  4.      * @param cn 指定Context下的名字   
  5.      * @param attMap 包含List key为属性名称,当属性为多值时   
  6.      * value 为包含多值的List,为单值时,为包含单值的String类型   
  7.      * @throws BaseException   
  8.      * @throws NamingException   
  9.      */    
  10.     public static void modifyAttributes(DirContext context, String cn,    
  11.                                         Map attMap) throws    
  12.             BaseException, NamingException {    
  13.   
  14.         // 参数为空    
  15.         if (context == null) {    
  16.             String[] args = {    
  17.                             "context"};    
  18.             // 打印错误日志    
  19.             StringBuffer msglog = new StringBuffer(    
  20.                     "empty invoke parameter context NULL ");    
  21.             log.error(msglog.toString());    
  22.             throw new BaseException("error.common.parameter.empty", args);    
  23.         }    
  24.   
  25.         // 参数为空    
  26.         if (attMap == null) {    
  27.             String[] args = {    
  28.                             "attMap"};    
  29.             // 打印错误日志    
  30.             StringBuffer msglog = new StringBuffer(    
  31.                     "empty invoke parameter attMap NULL ");    
  32.             log.error(msglog.toString());    
  33.             throw new BaseException("error.common.parameter.empty", args);    
  34.         }    
  35.         // 参数为空    
  36.         if (StringUtils.isEmpty(cn)) {    
  37.             String[] args = {    
  38.                             "cn"};    
  39.             // 打印错误日志    
  40.             StringBuffer msglog = new StringBuffer(    
  41.                     "empty invoke parameter cn NULL ");    
  42.             log.error(msglog.toString());    
  43.             throw new BaseException("error.common.parameter.empty", args);    
  44.         }    
  45.   
  46.         // 为空,退出    
  47.         if (attMap.isEmpty()) {    
  48.             return;    
  49.         }    
  50.         // 取所有的属性key    
  51.         Set keySet = attMap.keySet();    
  52.         Iterator keyIterator = keySet.iterator();    
  53.         Attributes attrs = new BasicAttributes();    
  54.         // 迭代所有的属性key    
  55.         while (keyIterator.hasNext()) {    
  56.             // 取下一个属笥    
  57.             String key = (String) keyIterator.next();    
  58.             Attribute att = null;    
  59.             Object valueObj = attMap.get(key);    
  60.   
  61.             if (valueObj instanceof List) {    
  62.                 // 为List ,为多值属性    
  63.                 att = new BasicAttribute(key);    
  64.                 List valueList = (List) valueObj;    
  65.                 // 加入多值属性    
  66.                 for (int i = 0; i < valueList.size(); i++) {    
  67.                     att.add(valueList.get(i));    
  68.                 }    
  69.             } else if (valueObj instanceof String) {    
  70.                 att = new BasicAttribute(key, valueObj);    
  71.             }    
  72.             // 加入    
  73.             attrs.put(att);    
  74.         }    
  75.         context.modifyAttributes(cn, DirContext.REPLACE_ATTRIBUTE, attrs);    
  76.         // context.close();    
  77.     }    
  78.   
  79.     //    
  80.     /**   
  81.      * 获取连接的DirContext中指定Context下的指定属性   
  82.      * @param context 连接的DirContext   
  83.      * @param cn  指定Context的名称   
  84.      * @param attNameList 要取的属性的名称List   
  85.      * @return Map包含List ,key 为属性的名称,当属性值为多值时,Value为List类型,   
  86.      * 否则,value 为String 类型   
  87.      * @throws NamingException   
  88.      */    
  89.     public static Map getAttributes(DirContext context, String cn,    
  90.                                     List attNameList) throws NamingException {    
  91.         Map attsMap = new HashMap();    
  92.         Attributes results = null;    
  93.         List attValList = null;    
  94.         String attrId = null;    
  95.   
  96.         if (attNameList == null) {    
  97.             results = context.getAttributes(cn);    
  98.         } else {    
  99.             if (!attNameList.isEmpty()) {    
  100.                 // results = context.getAttributes(cn);    
  101.                 String[] stTemp = new String[attNameList.size()];    
  102. /// 以下方法性能太低     
  103. //                for (int i = 0; i < attNameList.size(); i++) {    
  104. //                    stTemp[i] = (String) attNameList.get(i);    
  105. //                }    
  106. //                results = context.getAttributes(cn,    
  107. //                                                stTemp);    
  108. ///    
  109.                 // 比较高性能的List 转为 数组的方法    
  110.                 results = context.getAttributes(cn,    
  111.                                                 (String[]) (attNameList.toArray(stTemp)));    
  112.             }    
  113.         }    
  114.         for (int i = 0; i < attNameList.size(); i++) {    
  115.             Attribute attr = results.get((String) attNameList.get(i));    
  116.             attrId = (String) attNameList.get(i);    
  117.             if (attr != null) {    
  118.                 if (attr.size() > 0) {    
  119.                     NamingEnumeration vals = attr.getAll();    
  120.                     if (vals == null) {    
  121.                         continue;    
  122.                     }    
  123.                     Object obj1 = vals.nextElement();    
  124.                     if (obj1 == null) {    
  125.                         continue;    
  126.                     }    
  127.                     // 迭代这个属性的所有属性值    
  128.                     while (vals.hasMoreElements()) {    
  129.                         if (attValList == null) {    
  130.                             attValList = new ArrayList();    
  131.                             attValList.add(obj1);    
  132.                         }    
  133.                         attValList.add(vals.nextElement());    
  134.                     }    
  135.                     // 当属性为单值域时,存为字符串    
  136.                     // 当属性为多值域时,存为包含多值域的List    
  137.                     if (attValList != null) {    
  138.                         attsMap.put(attrId, attValList);    
  139.                         // 清空    
  140.                         attValList = null;    
  141.                     } else {    
  142.                         attsMap.put(attrId, obj1);    
  143.                     }    
  144.                 }    
  145.             }    
  146.         }    
  147.         // context.close();    
  148.         return attsMap;    
  149.     }    
  150.   
  151.     /**   
  152.      * 在当前连接的DirContext 获取指定Context下的指定属性名称的所有属性值(一个或多个值)   
  153.      * @param context 连接的DirContext   
  154.      * @param cn  指定Context的cn名   
  155.      * @param attName 属性名称   
  156.      * @return 返回包括属性值的List 注意,当属性只有一个值时,返回的List长度为1,当属性   
  157.      * 是多值属性时,返回List长度为属性值的数目   
  158.      * @throws NamingException   
  159.      */    
  160.     public static List getAttributeValues(DirContext context, String cn,    
  161.                                           String attName) throws    
  162.             NamingException {    
  163.         List attValList = new ArrayList();    
  164.         List attNameList = new ArrayList();    
  165.         attNameList.add(attName);    
  166.         Map attMap = null;    
  167.         attMap = getAttributes(context, cn, attNameList);    
  168.   
  169.         if (attMap != null) {    
  170.             Object attValObj = attMap.get(attName);    
  171.             if (attValObj instanceof String) {    
  172.                 attValList.add((String) attValObj);    
  173.             } else if (attValObj instanceof List) {    
  174.                 attValList = ((List) attValObj);    
  175.             }    
  176.         }    
  177.         // context.close();    
  178.         return attValList;    
  179.     }    
  180.   
  181.   
  182.     /**   
  183.      * 获取角色的相关信息   
  184.      * @param context DirContext   
  185.      * @param cn String   
  186.      * @param attName String   
  187.      * @return String   
  188.      * @throws NamingException   
  189.      */    
  190.     public static String getRoleAttributeValues(DirContext context, String cn,    
  191.                                           String attName) throws    
  192.             NamingException {    
  193.         String result = "";    
  194.         List attNameList = new ArrayList();    
  195.         attNameList.add(attName);    
  196.         Map attMap = null;    
  197.         attMap = getAttributes(context, cn, attNameList);    
  198.   
  199.         if (attMap != null) {    
  200.             Object attValObj = attMap.get(attName);    
  201.             result = (String)attValObj;    
  202.         }    
  203.         return result;    
  204.     }    
  205.   
  206.     /**   
  207.      * 根据条件查找指定CN的Context下的一层所有属性   
  208.      * @param context 连接了的DirContext   
  209.      * @param cn 要查询的BaseCN名称   
  210.      * @param filter 要查询的过滤字符串   
  211.      * @return 符合查询结果的List   
  212.      * @throws NamingException   
  213.      */    
  214.     public static List searchContextOne(DirContext context, String cn,    
  215.                                         String filter) throws    
  216.             NamingException {    
  217.         List resultList = new ArrayList();    
  218.         Map resultRowMap = null;    
  219.         List attValList = null;    
  220.         String attValStr = null;    
  221.         // 实例化一个搜索器    
  222.         SearchControls constraints = new SearchControls();    
  223.         // 设置搜索器的搜索范围    
  224.         constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);    
  225.         // 在基目录中搜索条件为Env.MY_FILTER的所有属性 注意:这里返回是的所有的条目集合    
  226.         NamingEnumeration results    
  227.                 = context.search(cn, filter, constraints);    
  228.   
  229.         // 打印条目的识别名(DN)及其所有的属性名,值    
  230.         while (results != null && results.hasMore()) {    
  231.             // 取一个条目    
  232.             SearchResult si = (SearchResult) results.next();    
  233.   
  234.             // 获取条目的所有属性集合    
  235.             Attributes attrs = si.getAttributes();    
  236.             if (attrs != null) {    
  237.                 String attrId = null;    
  238.                 // 一行数据    
  239.                 resultRowMap = new HashMap();    
  240.                 // 打印所有属性    
  241.                 for (NamingEnumeration ae = attrs.getAll();    
  242.                                             ae.hasMoreElements(); ) {    
  243.                     // 获取一个属性    
  244.                     Attribute attr = (Attribute) ae.next();    
  245.                     attrId = attr.getID();    
  246.                     Enumeration vals = attr.getAll();    
  247.                     if (vals == null) {    
  248.                         continue;    
  249.                     }    
  250.                     Object obj1 = vals.nextElement();    
  251.                     if (obj1 == null) {    
  252.                         continue;    
  253.                     }    
  254.                     // 迭代这个属性的所有属性值    
  255.                     while (vals.hasMoreElements()) {    
  256.                         if (attValList == null) {    
  257.                             attValList = new ArrayList();    
  258.                             attValList.add(obj1);    
  259.                         }    
  260.                         attValList.add(vals.nextElement());    
  261.                     }    
  262.                     // 当属性为单值域时,存为字符串    
  263.                     // 当属性为多值域时,存为包含多值域的List    
  264.                     if (attValList != null) {    
  265.                         resultRowMap.put(attrId, attValList);    
  266.                         // 清空    
  267.                         attValList = null;    
  268.                     } else {    
  269.                         resultRowMap.put(attrId, obj1);    
  270.                     }    
  271.   
  272.                 }    
  273.             }    
  274.             resultList.add(resultRowMap);    
  275.         }    
  276.         return resultList;    
  277.     }    
  278.   
  279.     /**   
  280.      * 根所条件查找指定CN的Context下的子树下的所有属性   
  281.      * @param context 连接了的DirContext   
  282.      * @param cn 要查询的BaseCN名称   
  283.      * @param filter 要查询的过滤字符串   
  284.      * @return 符合查询结果的List   
  285.      * @throws NamingException   
  286.      */    
  287.     public static List searchContextSub(DirContext context, String cn,    
  288.                                         String filter) throws    
  289.             NamingException {    
  290.         List resultList = new ArrayList();    
  291.         Map resultRowMap = null;    
  292.         List attValList = null;    
  293.         // 实例化一个搜索器    
  294.         SearchControls constraints = new SearchControls();    
  295.         // 设置搜索器的搜索范围    
  296.         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);    
  297.         // 在基目录中搜索条件为Env.MY_FILTER的所有属性 注意:这里返回是的所有的条目集合    
  298.         NamingEnumeration results    
  299.                 = context.search(cn, filter, constraints);    
  300.   
  301.         // 打印条目的识别名(DN)及其所有的属性名,值    
  302.         while (results != null && results.hasMore()) {    
  303.             // 取一个条目    
  304.             SearchResult si = (SearchResult) results.next();    
  305.   
  306.             // 获取条目的所有属性集合    
  307.             Attributes attrs = si.getAttributes();    
  308.             if (attrs != null) {    
  309.                 String attrId = null;    
  310.                 // 一行数据    
  311.                 resultRowMap = new HashMap();    
  312.                 // 打印所有属性值    
  313.                 for (NamingEnumeration ae = attrs.getAll();    
  314.                                             ae.hasMoreElements(); ) {    
  315.                     // 获取一个属性    
  316.                     Attribute attr = (Attribute) ae.next();    
  317.                     attrId = attr.getID();    
  318.                     Enumeration vals = attr.getAll();    
  319.                     if (vals == null) {    
  320.                         continue;    
  321.                     }    
  322.                     Object obj1 = vals.nextElement();    
  323.                     if (obj1 == null) {    
  324.                         continue;    
  325.                     }    
  326.                     // 迭代这个属性的所有属性值    
  327.                     while (vals.hasMoreElements()) {    
  328.                         if (attValList == null) {    
  329.                             attValList = new ArrayList();    
  330.                             attValList.add(obj1);    
  331.                         }    
  332.                         attValList.add(vals.nextElement());    
  333.                     }    
  334.                     // 当属性为单值域时,存为字符串    
  335.                     // 当属性为多值域时,存为包含多值域的List    
  336.                     if (attValList != null) {    
  337.                         resultRowMap.put(attrId, attValList);    
  338.                         // 清空    
  339.                         attValList = null;    
  340.                     } else {    
  341.                         resultRowMap.put(attrId, obj1);    
  342.                     }    
  343.                 }    
  344.             }    
  345.             resultList.add(resultRowMap);    
  346.         }    
  347.         return resultList;    
  348.     }    
  349.   
  350.   
  351.     /**   
  352.      * 查找指定CN的Context下的子树下的指定属性   
  353.      * @param context DirContext   
  354.      * @param cn String   
  355.      * @param filter String   
  356.      * @param returnedAtts String[] 属性名字数组   
  357.      * @return List   
  358.      * @throws NamingException   
  359.      */    
  360.     public static List searchContextSub(DirContext context, String cn,    
  361.                                         String filter, String[] returnedAtts) throws    
  362.             NamingException {    
  363.         List resultList = new ArrayList();    
  364.         String attrId = null;    
  365.         List attValList = null;    
  366.         Map resultRowMap = null;    
  367.         // 实例化一个搜索器    
  368.         SearchControls constraints = new SearchControls();    
  369.         // 设置搜索器的搜索范围    
  370.         constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);    
  371.         // String[] returnedAtts = {"uniquemember"};    
  372.         constraints.setReturningAttributes(returnedAtts);    
  373.         // 条目    
  374.         NamingEnumeration results    
  375.                 = context.search(cn, filter, constraints);    
  376.   
  377.         // 迭代所有的条目    
  378.         while (results != null && results.hasMore()) {    
  379.             // 取一个条目    
  380.             SearchResult si = (SearchResult) results.next();    
  381.             resultRowMap = new HashMap();    
  382.             // 获取条目的指定返回的属性    
  383.             Attributes attrs = si.getAttributes();    
  384.             if (attrs != null) {    
  385.                 // 迭代所有属性值    
  386.                 for (NamingEnumeration ae = attrs.getAll();    
  387.                                             ae.hasMoreElements(); ) {    
  388.   
  389.                     // 获取一个属性    
  390.                     Attribute attr = (Attribute) ae.next();    
  391.                     attrId = attr.getID();    
  392.                     Enumeration vals = attr.getAll();    
  393.                     if (vals == null) {    
  394.                         continue;    
  395.                     }    
  396.                     // 迭代这个属性的所有属性值    
  397.                     while (vals.hasMoreElements()) {    
  398.                         if (attValList == null) {    
  399.                             attValList = new ArrayList();    
  400.                         }    
  401.                         attValList.add(vals.nextElement());    
  402.                     }    
  403.                     // 当属性为单值域时,存为字符串    
  404.                     // 当属性为多值域时,存为包含多值域的List    
  405.                     if (attValList != null) {    
  406.                         resultRowMap.put(attrId, attValList);    
  407.                         // 清空    
  408.                         attValList = null;    
  409.                     }    
  410.                 }    
  411.             }    
  412.             resultList.add(resultRowMap);    
  413.         }    
  414.         return resultList;    
  415.     }    
  416.   
  417.     /**   
  418.      * 查找指定CN的Context下的一层指定属性   
  419.      * @param context DirContext   
  420.      * @param cn String   
  421.      * @param filter String   
  422.      * @param returnedAtts String[] 属性名字数组   
  423.      * @return List   
  424.      * @throws NamingException   
  425.      */    
  426.     public static List searchContextOne(DirContext context, String cn,    
  427.                                         String filter, String[] returnedAtts) throws    
  428.             NamingException {    
  429.         List resultList = new ArrayList();    
  430.         String attrId = null;    
  431.         List attValList = null;    
  432.         Map resultRowMap = null;    
  433.         // 实例化一个搜索器    
  434.         SearchControls constraints = new SearchControls();    
  435.         // 设置搜索器的搜索范围    
  436.         constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);    
  437.         // String[] returnedAtts = {"uniquemember"};    
  438.         constraints.setReturningAttributes(returnedAtts);    
  439.         // 条目    
  440.         NamingEnumeration results    
  441.                 = context.search(cn, filter, constraints);    
  442.   
  443.         // 迭代所有的条目    
  444.         while (results != null && results.hasMore()) {    
  445.             // 取一个条目    
  446.             SearchResult si = (SearchResult) results.next();    
  447.             resultRowMap = new HashMap();    
  448.             // 获取条目的指定返回的属性    
  449.             Attributes attrs = si.getAttributes();    
  450.             if (attrs != null) {    
  451.                 // 迭代所有属性值    
  452.                 for (NamingEnumeration ae = attrs.getAll();    
  453.                                             ae.hasMoreElements(); ) {    
  454.   
  455.                     // 获取一个属性    
  456.                     Attribute attr = (Attribute) ae.next();    
  457.                     attrId = attr.getID();    
  458.                     Enumeration vals = attr.getAll();    
  459.                     if (vals == null) {    
  460.                         continue;    
  461.                     }    
  462.                     Object obj1 = vals.nextElement();    
  463.                     if (obj1 == null) {    
  464.                         continue;    
  465.                     }    
  466.                     // 迭代这个属性的所有属性值    
  467.                     while (vals.hasMoreElements()) {    
  468.                         if (attValList == null) {    
  469.                             attValList = new ArrayList();    
  470.                             attValList.add(obj1);    
  471.                         }    
  472.                         attValList.add(vals.nextElement());    
  473.                     }    
  474.                     // 当属性为单值域时,存为字符串    
  475.                     // 当属性为多值域时,存为包含多值域的List    
  476.                     if (attValList != null) {    
  477.                         resultRowMap.put(attrId, attValList);    
  478.                         // 清空    
  479.                         attValList = null;    
  480.                     } else {    
  481.                         resultRowMap.put(attrId, obj1);    
  482.                     }    
  483.                 }    
  484.             }    
  485.             resultList.add(resultRowMap);    
  486.         }    
  487.         return resultList;    
  488.     }    
  489.   
  490.   
  491.     /**   
  492.         * 在当前的连接DirContext 删除 指定Context 下的 一个属性里面包含的子属性   
  493.         * @param context 连接后的DirContext   
  494.         * @param cn 指定Context的名称   
  495.         * @param attList 包含要删除的属性的名称   
  496.         * @throws BaseException   
  497.         * @throws NamingException   
  498.         */    
  499.        public static void deleteInAttributes(DirContext ctx, String userDN,    
  500.                                              List attList,String flag) throws NamingException {    
  501.            if (attList == null || attList.size() == 0) {    
  502.                return;    
  503.            } else {    
  504.                int size = attList.size();    
  505.                ModificationItem[] mods = new ModificationItem[size];    
  506.                for (int i = 0; i < size; i++) {    
  507.                    Attribute att = null;    
  508.                    mods[i] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,    
  509.                                                   new BasicAttribute(    
  510.                            flag, (String) attList.get(i)));    
  511.                }    
  512.                ctx.modifyAttributes(userDN, mods);    
  513.            }    
  514.     }    
  515.   
  516.     /**   
  517.      * 创建一个连接,通过捕捉Exception来确定该用户是否存在于目标ldap中   
  518.      * @param configDto ConfigDto   
  519.      * @param uid String   
  520.      * @param password char[]   
  521.      * @return boolean   
  522.      * @throws NamingException   
  523.      */    
  524.     public static boolean authenticate(ConfigDto configDto, String uid, char[] password) throws    
  525.             NamingException {    
  526.         Hashtable mEnvironment = new Hashtable();    
  527.         DirContext mContext = null;    
  528.         //创建连接    
  529.         mEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,    
  530.                          configDto.getEnvfactory());    
  531.         mEnvironment.put(Context.PROVIDER_URL, configDto.getEnvurl());    
  532.         mEnvironment.put(Context.SECURITY_AUTHENTICATION, "simple");    
  533.         mEnvironment.put(Context.SECURITY_PRINCIPAL,    
  534.                          Constants.LDAP_PEOPLE_ATTRIBUTE_UID + "=" + uid + "," +    
  535.                          configDto.getEnvPeopleLoc());    
  536.         mEnvironment.put(Context.SECURITY_CREDENTIALS, password);    
  537.         try {    
  538.             mContext = new InitialDirContext(mEnvironment);    
  539.             log.debug("user:"+uid+" login!");    
  540.             return true;    
  541.         } catch (AuthenticationException ex) {    
  542.             log.error("user:"+uid+" don't login because of wrong user name or password!");    
  543.             return false;    
  544.         }    
  545.     }   

转载于:https://www.cnblogs.com/kungfupanda/archive/2011/02/21/1959925.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值