<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(2308) 評論(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有問題了  回復  更多評論
      

    主站蜘蛛池模板: 久久亚洲最大成人网4438| 亚洲欧洲日产国码在线观看| 中文字幕免费在线看| 在线观看国产情趣免费视频| 色天使亚洲综合在线观看| 一二三四视频在线观看中文版免费| 亚洲大片免费观看| 免费国产黄线在线观看| 亚洲人成77777在线观看网| 在线观看成人免费视频| 在线播放亚洲精品| 久久久久亚洲AV成人网| 人妻在线日韩免费视频| 337p日本欧洲亚洲大胆艺术| 久久www免费人成看片| 日本亚洲色大成网站www久久| 国产免费人成视频在线观看| 国产成人高清亚洲一区91| 国产亚洲一区二区三区在线不卡| 999zyz**站免费毛片| 亚洲欧洲高清有无| 精品久久免费视频| 不卡视频免费在线观看| 亚洲无限乱码一二三四区| 国产嫩草影院精品免费网址| 国产免费久久精品丫丫| 久久久亚洲裙底偷窥综合| 女人18毛片a级毛片免费视频| 一级毛片成人免费看a| 亚洲日本在线观看| 国产精品免费一级在线观看| 中国一级全黄的免费观看| 亚洲理论片在线中文字幕| 卡一卡二卡三在线入口免费| 男人天堂免费视频| 亚洲综合在线一区二区三区| 中文字幕亚洲日本岛国片| 91精品免费在线观看| 一区二区三区精品高清视频免费在线播放 | 黑人粗长大战亚洲女2021国产精品成人免费视频 | www在线观看免费视频|