Kimi's NutShell

          我荒廢的今日,正是昨日殞身之人祈求的明日

          BlogJava 新隨筆 管理
            141 Posts :: 0 Stories :: 75 Comments :: 0 Trackbacks

          建立LDAP服務(wù)器的連接

          package com.prime.mypackage;

          import java.io.File;
          import java.io.FileInputStream;

          import java.util.Hashtable;
          import java.util.Properties;

          import javax.naming.Context;
          import javax.naming.directory.DirContext;
          import javax.naming.directory.InitialDirContext;


          public class LdapQuery {
          ??? /*服務(wù)提供者*/
          ??? private static String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

          ??? /*LDAP連接*/
          ??? private DirContext dirContext;

          ??? /*參數(shù)列表*/
          ??? private Hashtable enviroment;

          ??? /**
          ??? * 構(gòu)造函數(shù)
          ??? */
          ??? public LdapQuery() {
          ??????? dirContext = null;
          ??????? enviroment = new Hashtable();
          ??? }

          ??? public static void main(String[] args){
          ??? ?LdapQuery lp=new LdapQuery();
          ??? ?try{
          ??? ?lp.init("cn=orcladmin","abc123");
          ??? ?}catch(Exception e){
          ??? ??e.printStackTrace();
          ??? ?}
          ??? }

          ??? /**
          ??? * 讀取配置文件,連接LDAP服務(wù)器
          ??? * @throws LdapException
          ??? */
          ??? public? DirContext init(String username, String password)
          ??????? throws Exception {
          ??????? try {
          ??????????? Properties config = new Properties();
          ??????????? File f = new File("C:/Projects/Java/ldap.property");

          ??????????? if (!f.exists()) {
          ??????????????? throw new Exception("沒(méi)發(fā)現(xiàn)配置文件");
          ??????????? }

          ??????????? FileInputStream configFile = new FileInputStream(f);
          ??????????? config.load(configFile);

          ??????????? String host = config.getProperty("host");
          ??????????? String port = config.getProperty("port");
          ??????????? configFile.close();

          ??????????? enviroment.put(Context.INITIAL_CONTEXT_FACTORY, CTX_FACTORY);
          ??????????? enviroment.put(Context.PROVIDER_URL, "ldap://" + host + ":" + port);

          ??????????? if (password != null) {
          ??????????????? enviroment.put(Context.SECURITY_AUTHENTICATION, "simple");
          ??????????????? enviroment.put(Context.SECURITY_PRINCIPAL, username);
          ??????????????? enviroment.put(Context.SECURITY_CREDENTIALS, password);
          ??????????? }

          ??????????? dirContext = new InitialDirContext(enviroment);

          ??????????? if (dirContext != null) {
          ??????????????? System.out.println("Connect");

          ??????????????? return dirContext;
          ??????????? }

          ??????????? return null;
          ??????? } catch (Exception e) {
          ??????????? throw new Exception("LdapQuery.init:" + e.toString());
          ??????? }
          ??? }
          }

          做第一個(gè)動(dòng)作 add()
          package com.prime.mypackage;
          import java.util.Iterator;
          import java.util.Map;
          import java.util.Set;
          import javax.naming.directory.DirContext;
          import java.util.Hashtable;
          import java.util.Enumeration;
          import javax.naming.Context;
          import javax.naming.NamingException;
          import javax.naming.directory.DirContext;
          import javax.naming.directory.InitialDirContext;
          import javax.naming.directory.SearchControls ;
          import javax.naming.NamingEnumeration;
          import javax.naming.directory.SearchResult;
          import javax.naming.directory.Attributes ;
          import javax.naming.directory.Attribute;
          import javax.naming.directory.BasicAttributes;
          import javax.naming.directory.BasicAttribute;
          import javax.naming.directory.ModificationItem;
          import java.lang.reflect.Method;
          import java.io.BufferedReader;
          import java.io.InputStreamReader;

          ?

          public class LdapAction
          {

          ? DirContext ctx=null;
          ? public static void main(String[] args)
          ? {
          ??? LdapAction LA=new LdapAction();
          ??? LA.add();
          ? }
          ? public LdapAction()
          ? {
          ? LdapQuery query=new LdapQuery();
          ?
          ? try{
          ?? ctx=query.init("cn=orcladmin","abc123");
          ? }catch(Exception e)
          ? {
          ??? e.printStackTrace();
          ? }
          ? }
          ? public void add(){
          ???? try{
          ?? String newUserName = "test_add";
          ?? BasicAttributes attrs = new BasicAttributes();
          ?? BasicAttribute objclassSet = new BasicAttribute("objectclass");
          ?? BasicAttribute pass=new BasicAttribute("userpassword");
          ?? pass.add("123qweasd");
          ?? objclassSet.add("top");
          ?? objclassSet.add("person");
          ?? objclassSet.add("organizationalPerson");
          ?? objclassSet.add("inetOrgPerson");
          ?? objclassSet.add("orcluser");
          ?? objclassSet.add("orcluserV2");
          ?? attrs.put(pass);
          ?? attrs.put(objclassSet);
          ?? attrs.put("sn", newUserName);
          ?? attrs.put("uid", newUserName);
          ?? attrs.put("cn", newUserName);
          ?? ctx.createSubcontext("uid=" + newUserName+",cn=users,dc=dev,dc=daphne,dc=com,dc=cn", attrs);
          ? }catch(Exception e){
          ?? System.out.println("Exception in add():"+e);
          ? }
          ??? }


          }
          待敘~

          posted on 2006-07-06 16:57 Kimi 閱讀(2309) 評(píng)論(10)  編輯  收藏 所屬分類: Java

          評(píng)論

          # re: 用JAVA刺穿LDAP (一) 2007-03-09 10:28 hrs
          請(qǐng)問(wèn)在add操作中有沒(méi)有異常javax.naming.OperationNotSupportedException: [LDAP: error code 53 - modification of subschema subentry not supported];
          拋出啊

          請(qǐng)解釋一下,非常謝謝  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-09 11:04 Kemi
          @hrs

          沒(méi)有這樣的錯(cuò)誤報(bào)過(guò)。
          你找一下是哪段代碼出問(wèn)題了?  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-09 11:08 Kemi
          http://javaresearch.org/article/42203.htm

          這篇文章寫(xiě)的很清楚,你也可以參考參考  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-12 08:47 hrs
          問(wèn)題是出在ctx.createSubcontext這個(gè)調(diào)用上,麻煩你看一下代碼吧,如下

          import javax.naming.directory.*;
          import javax.naming.*;
          import java.util.Hashtable;
          public class add {
          public add() {
          }
          public static void main(String[] args) {
          String password = "cm";
          String basedn = "dc=cm,dc=com";
          DirContext ctx = null;
          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/"+basedn );
          env.put(Context.SECURITY_PRINCIPAL, "cn=root,"+basedn );
          env.put(Context.SECURITY_CREDENTIALS, password);

          try {
          ctx = new InitialDirContext(env);
          System.out.println("認(rèn)證成功");
          }
          catch (javax.naming.AuthenticationException e) {
          System.out.println("認(rèn)證失敗");
          }
          catch (Exception e) {
          System.out.println("認(rèn)證出錯(cuò):" + e);
          }
          try{
          DirContext schemaCtx = ctx.getSchema("");
          BasicAttributes attrs = new BasicAttributes(true);
          attrs.put("NAME", "test");
          attrs.put("NUMERICOID", "1.3.6.1.4.1.7914.1.2.1.16");
          attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.15");
          attrs.put("SINGLE-VALUE", "TRUE");
          schemaCtx.createSubcontext("AttributeDefinition/test", attrs);
          System.out.println("ok");
          }catch(Exception e){
          System.out.println("Exception in add():"+e);
          }
          }
          }

          提示:
          認(rèn)證成功
          Exception in add():javax.naming.OperationNotSupportedException: [LDAP: error code 53 - modification of subschema subentry not supported]; remaining name ''

          應(yīng)該是schemaCtx.createSubcontext("AttributeDefinition/test", attrs);處拋異常了,請(qǐng)指教,謝謝
            回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-12 10:54 Kemi
          有可能是LDAP架包的版本問(wèn)題,請(qǐng)參考

          http://www.openldap.org/lists/openldap-bugs/200604/msg00017.html  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-12 10:56 Kemi
          IBM 系列課程上面也有,不過(guò)沒(méi)有提及這樣的錯(cuò)誤


          http://publib.boulder.ibm.com/tividd/td/IBMDS/guide322/en_US/HTML/Guide.html


          DirContext schemaCtx = ctx.getSchema("");
          BasicAttributes attrs = new BasicAttributes();
          attrs.put("NAME", "javaObject");
          attrs.put("NUMERICOID", "1.3.6.1.4.1.42.2.27.4.2.2");
          Attribute may = new BasicAttribute("MAY");
          may.add("javaClassName");
          may.add("javaSerializedObject");
          attrs.put(may);
          attrs.put("DESC", "Serialized Java object");
          attrs.put("AUXILIARY", "true");
          attrs.put("SUP", "top");
          schemaCtx.createSubcontext("ClassDefinition/javaObject", attrs);  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-12 10:58 Kemi
          另外,我對(duì)于 String AddDn = "uid=" + user + "," + machineryPath;
          dctx.createSubcontext(AddDn, attrs);
          ,你關(guān)注下 AddDn 是否需要重新 set  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-12 14:41 hrs
          非常感謝,我再試試  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-14 08:14 hrs
          你能再幫一下嗎,問(wèn)題還沒(méi)有解決啊,還是在schemaCtx.createSubcontext("AttributeDefinition/test", attrs);處拋異常了,請(qǐng)指教,謝謝  回復(fù)  更多評(píng)論
            

          # re: 用JAVA刺穿LDAP (一) 2007-03-14 08:55 Kemi
          我的例子里面
          ctx.createSubcontext("uid=" + newUserName+",cn=users,dc=dev,dc=daphne,dc=com,dc=cn", attrs);
          沒(méi)有問(wèn)題。你肯定是AttributeDefinition/test有問(wèn)題了  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 深圳市| 尼玛县| 合肥市| 延边| 宁化县| 安岳县| 织金县| 清水县| 保定市| 玉树县| 商南县| 南丹县| 五莲县| 东乌珠穆沁旗| 西昌市| 麻江县| 佛山市| 涿鹿县| 马龙县| 屯留县| 天柱县| 余姚市| 丹棱县| 太仆寺旗| 江华| 屯留县| 南丰县| 安塞县| 丰原市| 南京市| 高唐县| 和平县| 普安县| 万盛区| 遵义市| 武冈市| 铜鼓县| 乡宁县| 静海县| 崇州市| 玉山县|