Kimi's NutShell

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

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

          建立LDAP服務器的連接

          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 {
          ??? /*服務提供者*/
          ??? private static String CTX_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";

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

          ??? /*參數列表*/
          ??? private Hashtable enviroment;

          ??? /**
          ??? * 構造函數
          ??? */
          ??? 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服務器
          ??? * @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("沒發現配置文件");
          ??????????? }

          ??????????? 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());
          ??????? }
          ??? }
          }

          做第一個動作 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) 評論(10)  編輯  收藏 所屬分類: Java

          評論

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

          請解釋一下,非常謝謝  回復  更多評論
            

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

          沒有這樣的錯誤報過。
          你找一下是哪段代碼出問題了?  回復  更多評論
            

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

          這篇文章寫的很清楚,你也可以參考參考  回復  更多評論
            

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

          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("認證成功");
          }
          catch (javax.naming.AuthenticationException e) {
          System.out.println("認證失敗");
          }
          catch (Exception e) {
          System.out.println("認證出錯:" + 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);
          }
          }
          }

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

          應該是schemaCtx.createSubcontext("AttributeDefinition/test", attrs);處拋異常了,請指教,謝謝
            回復  更多評論
            

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

          http://www.openldap.org/lists/openldap-bugs/200604/msg00017.html  回復  更多評論
            

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


          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);  回復  更多評論
            

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

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

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

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

          主站蜘蛛池模板: 慈溪市| 滕州市| 阿巴嘎旗| 吴江市| 行唐县| 阳原县| 淳安县| 中山市| 宾川县| 广元市| 精河县| 荥经县| 色达县| 东源县| 三门县| 抚远县| 鄂温| 宣恩县| 华安县| 海晏县| 鄱阳县| 陇南市| 石城县| 镇远县| 溧水县| 双牌县| 高邑县| 宝清县| 新宁县| 五家渠市| 宁津县| 师宗县| 昂仁县| 台东市| 乌鲁木齐市| 武城县| 桐庐县| 洪泽县| 河津市| 清流县| 盘锦市|