posts - 23,comments - 12,trackbacks - 0
          轉(zhuǎn)自:http://www.webasp.net/article/13/12722_print.htm

          /* 1. 從LDAP服務(wù)器中提取常用名cn、可區(qū)分名字uid、密碼userpassword、Email地址mail
          * 其中使用Netscape LDAP服務(wù)器作為測試環(huán)境,使用simple認(rèn)證方式登錄LDAP服務(wù)器。
          * 2. 用命名‘a(chǎn)dmin’密碼是‘1’,整個程序使用SDK1.4.1中的JNDI標(biāo)準(zhǔn)接口。
          * 3. 為了配合DOMINO數(shù)據(jù)庫開發(fā),假設(shè)用戶登錄時候的IP地址已經(jīng)記錄在了字段uid中,并用‘,’隔開
          * 程序最終將打印一個包括所有用戶名,密碼,IP地址的字符串。
          * 4. 在處理分離用戶名和IP地址的時候,引入了正則表達(dá)式的使用。
          */
          package mm;

          //引入LDAP的包
          import java.lang.*;
          import java.util.Hashtable;
          import java.util.Enumeration;
          import javax.naming.*;
          import javax.naming.directory.*;
          //import mm.splitString;

          public class JNDISearch{
          public static String INITCTX = "com.sun.jndi.ldap.LdapCtxFactory"; //驅(qū)動
          public static String MY_HOST = "ldap://localhost:389"; //主機(jī)地址和端口
          public static String MY_SEARCHBASE = "o=airius.com"; //基點入口
          public static String MY_FILTER = "(mail=west)"; //過濾條件
          public static String MGR_DN="uid=admin,ou=Administrators,ou=TopologyManagement,o=NetscapeRoot"; //用戶名
          public static String MGR_PW="1"; //密碼
          public static String MY_ATTRS[] = {/*"cn","userpassword","mail",*/"cn"};
          //StringBuffer res = new StringBuffer(); //用來輸入名字,IP地址的對象
          public static String temp = new String();


          public String search() throws Exception{
          StringBuffer res = new StringBuffer();
          try{
          //建立連接
          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY,INITCTX);
          env.put(Context.PROVIDER_URL,MY_HOST);
          env.put(Context.SECURITY_AUTHENTICATION,"simple"); //使用簡單認(rèn)證來認(rèn)證用戶
          env.put(Context.SECURITY_PRINCIPAL,MGR_DN);
          env.put(Context.SECURITY_CREDENTIALS,MGR_PW);
          DirContext ctx = new InitialDirContext(env);

          //設(shè)置查詢范圍并開始查詢
          SearchControls constraints = new SearchControls();
          constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
          NamingEnumeration results = ctx.search(MY_SEARCHBASE,MY_FILTER,constraints);

          //打印查詢結(jié)果
          while (results != null && results.hasMore()){
          SearchResult sr = (SearchResult) results.next();
          //String dn = sr.getName();
          String dn = sr.getName()+","+MY_SEARCHBASE;
          System.out.println("==============================================");
          System.out.println("Distinguished Name is: "+dn);

          // 打印指定的字段//////////////////////////////////////////////////////////////////
          Attributes ar = ctx.getAttributes(dn,MY_ATTRS);
          if(ar==null) {
          //對應(yīng)的uid沒有多余的屬性
          System.out.println("Entry "+dn+" has none of the specified attributes\n");
          } else {
          //開始顯示對應(yīng)的字段
          for(int i=0;iAttribute attr = ar.get(MY_ATTRS[i]);
          if(attr!=null) {
          System.out.print(MY_ATTRS[i]+" : ");
          for(Enumeration vals = attr.getAll();
          vals.hasMoreElements(); ) {
          temp = (String)vals.nextElement();
          System.out.println("\t"+temp);
          res.append(temp+"/");
          }
          }
          System.out.println("\n");
          }
          ///////////////////////////////////////////////////////////////////////////////////


          /* 打印全部的字段///////////////////////////////////////////////////////////////////
          Attributes attrs = sr.getAttributes();
          for(NamingEnumeration ne = attrs.getAll();
          ne.hasMoreElements(); ){
          Attribute attr = (Attribute) ne.next();
          String attrID = attr.getID();
          System.out.println(attrID+": ");
          for(Enumeration vals = attr.getAll();vals.hasMoreElements(); ){
          System.out.println("\t"+vals.nextElement());
          }
          *//////////////////////////////////////////////////////////////////////////////////
          }
          }
          }catch (Exception e){
          e.printStackTrace();
          System.exit(1);
          }
          System.out.println(res.toString()+"\n\n\n\n");

          //splitString sp = new splitString();
          //System.out.println("一共有"+sp.splitString(res.toString()).length+"個返回"); //打印顯示結(jié)果,計算返回的數(shù)組值
          //return sp.splitString(res.toString());
          return res.toString();

          }

          ///////////////////////////////////////////////////////////////////////////////////////////
          // 使用正則表達(dá)式來分揀提取的字符串 ///////////////////////////////////
          ///////////////////////////////////////////////////////////////////////////////////////////

          }
          posted on 2005-10-27 16:30 my java 閱讀(469) 評論(0)  編輯  收藏 所屬分類: JNDI
          主站蜘蛛池模板: 灌阳县| 罗山县| 和林格尔县| 清水县| 洛阳市| 天镇县| 义乌市| 元阳县| 大埔区| 清河县| 澳门| 鲜城| 定南县| 山东省| 宜兴市| 渭源县| 玉林市| 金华市| 泊头市| 咸阳市| 诸城市| 南宫市| 湖州市| 尼玛县| 高邑县| 宁河县| 石城县| 南宁市| 夏河县| 三明市| 莆田市| 绩溪县| 襄樊市| 光山县| 遂宁市| 宁明县| 白河县| 琼海市| 灌云县| 葫芦岛市| 霍城县|