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 阅读(2294) 评论(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有问题了  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: