免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4688 | 回复: 6
打印 上一主题 下一主题

[ldap] 如何利用jndi向openldap里添加新的属性 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-04-01 16:54 |只看该作者 |倒序浏览
如何利用jndi向openldap里添加新的属性

下面是一个程序,但是有问题。
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 root = "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/" + root);
  env.put(Context.SECURITY_AUTHENTICATION, "simple");
  env.put(Context.SECURITY_PRINCIPAL, "cn=root," + root);
  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{

          Attributes attrs = new BasicAttributes(true); // Ignore case
          attrs.put("NAME", "system-createtime");
          attrs.put("NUMERICOID", "system-createtime-oid");//在openldap里,好像要求NUMERICOID是数字,比如像“1.3.6.1.4.1.7914.1.2.1.1”这样,而且不能有重复的,在sun one里是不是可以写成"system-createtime-oid"?

          attrs.put("DESC", "创建时间");
          if (true) {
            attrs.put("SINGLE-VALUE", "TRUE");
          }
                         attrs.put("SYNTAX", "1.3.6.1.4.1.1466.115.121.1.24");
         
              // Generalized Time
              DirContext newClass = ctx.createSubcontext("AttributeDefinition=system-createtime", attrs);

  System.out.println("ok");
  }catch(Exception e){
  System.out.println("Exception in add():"+e);
}
   }

}

出错提示是:Exception in add():javax.naming.InvalidNameException: AttributeDefinition=system-createtime: [LDAP: error code 34 - invalid DN]; remaining name 'AttributeDefinition=system-createtime'

论坛徽章:
0
2 [报告]
发表于 2006-04-01 16:55 |只看该作者
请问应该怎样添加NUMERICOID?谢谢:)

论坛徽章:
0
3 [报告]
发表于 2006-04-02 11:55 |只看该作者
把程序改了一下,但是还有问题。
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 ''

论坛徽章:
0
4 [报告]
发表于 2006-04-02 11:56 |只看该作者
那位高手能给指点一下?

论坛徽章:
0
5 [报告]
发表于 2006-04-03 19:15 |只看该作者
上网查了一下,发现好多人都遇到了和我一样的问题,我给openldap-software@OpenLDAP.org也发了邮件,可是没人理我。我在sun上下载了一个程序,运行了一下,和我的程序抛出的异常是一样的,看了看slapd.conf,也没有什么特别的设置,实在是有点晕。

论坛徽章:
0
6 [报告]
发表于 2006-04-03 19:16 |只看该作者
不是吧,我已经升级成侠客了?就因为我问的问题没有人回答吗?

论坛徽章:
0
7 [报告]
发表于 2006-08-24 13:57 |只看该作者
public boolean createUser(String machineryPath, String user,
                                String password, String mail, String mobile, String usertype,
                                String managemachinery, String displayname, String validateCode,
                                String address,String postalcode,String telphone,String shengshi,
                                String nvsex,String vc_Uip,String startupuser,String maxnum) {
                        DirContext dctx=null;
                        try {
                                dctx = (DirContext) myLdapConnection.get();//调用线程中现有的连接
                                if (dctx == null) {
                                        Hashtable env = new Hashtable();
                                        env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX);
                                        env.put(Context.PROVIDER_URL, MY_HOST);
                                        env.put(Context.SECURITY_AUTHENTICATION, MY_AUTH);
                                        env.put(Context.SECURITY_PRINCIPAL, MY_DN);
                                        env.put(Context.SECURITY_CREDENTIALS, MY_PW);
                                        dctx = new InitialDirContext(env);
                                        myLdapConnection.set(dctx);
                                }
                                //检查用户名是否已注册
                                Attributes SearchAttrs = new BasicAttributes(true);
                                SearchAttrs.put(new BasicAttribute("uid", user));
                                NamingEnumeration result = dctx.search(machineryPath, SearchAttrs);
                                //判断用户信息是否全,如果为空转为空值
                                if (password == null)
                                {
                                        password = "";
                                }
                                else if (mail == null)
                                {
                                        mail = "";
                                }
                                else if (mobile == null)
                                {
                                        mobile = "";
                                }
                                else if (displayname == null)
                                {
                                        displayname = "";
                                }
                                else if (validateCode == null)
                                {
                                        validateCode = "";
                                }
                                else if (address == null)
                                {
                                        address = "";
                                }
                                else if (postalcode == null)
                                {
                                        postalcode = "";
                                }
                                else if (telphone == null)
                                {
                                        telphone = "";
                                }
                                else if (shengshi == null)
                                {
                                        shengshi = "";
                                }
                                else if (nvsex == null)
                                {
                                        nvsex = "";
                                }
                                else if (vc_Uip == null)
                                {
                                        vc_Uip = "";
                                }
                                else if (startupuser == null)
                                {
                                        startupuser = "";
                                }
                                else if (maxnum == null)
                                {
                                        maxnum = "1";
                                }
                               
                               
                                if (result.hasMore())
                                {
                                        //用户名已注册
                                        return false;
                                }
                                else
                                {
                                        //用户名没有注册
                                        Attributes attrs = new BasicAttributes(true);
                                        //写用户名
                                        Attribute id = new BasicAttribute("uid");
                                        id.add(user);
                                        Attribute cn = new BasicAttribute("cn");
                                        cn.add(user);
                                        Attribute sn = new BasicAttribute("sn");
                                        sn.add(user);
                                        //写email
                                        Attribute email = new BasicAttribute("mail");
                                        email.add(mail);
                                       
                                        //写口令
                                        Attribute pw = new BasicAttribute("userPassword");
                                        pw.add(password);
                                       
                                        //写手机号
                                        Attribute mob = new BasicAttribute("mobile");
                                        mob.add(mobile);
                                       
                                        //写用户类型
                                        Attribute type = new BasicAttribute("l");
                                        type.add(usertype);
                                       
                                        //写机构管理员管理的机构路径
                                        Attribute manage = new BasicAttribute("description");
                                        manage.add(managemachinery);
                                       
                                        //写名字
                                        Attribute nam = new BasicAttribute("displayName");
                                        nam.add(displayname);

                                        Attribute mes = new BasicAttribute("title");
                                        mes.add(validateCode);
                                       
                                        //USERCODE
                                        Attribute machinery = new BasicAttribute("employeeType");
                                        machinery.add(machineryPath);
                                       
                                        //部门
                                        Attribute objectclass = new BasicAttribute("objectclass");
                                        objectclass.add("inetOrgPerson");

                                        //以下是扩展
                                        //写联系地址
                                        Attribute addr = new BasicAttribute("x121Address");
                                        addr.add(address);
                                       
                                        //写邮编
                                        Attribute post = new BasicAttribute("postalCode");
                                        post.add(postalcode);
                                       
                                        //写固定电话
                                        Attribute tel = new BasicAttribute("telephoneNumber");
                                        tel.add(telphone);
                                       
                                        //写生日
                                        Attribute sr = new BasicAttribute("o");
                                        sr.add(shengshi);
                                       
                                        //写性别
                                        Attribute nn = new BasicAttribute("audio");
                                        nn.add(nvsex);
                                       
                                        //写ip
                                        Attribute ip = new BasicAttribute("seeAlso");
                                        ip.add(vc_Uip);
                                       
                                        //写开启状态
                                        Attribute stuts = new BasicAttribute("initials");
                                        stuts.add(startupuser);
                                       
                                        //写开启状态
                                        Attribute num = new BasicAttribute("roomNumber");
                                        num.add(maxnum);
                                       
                                        attrs.put(id);
                                        attrs.put(cn);
                                        attrs.put(sn);
                                        attrs.put(pw);
                                        attrs.put(email);
                                        attrs.put(nam);
                                        attrs.put(mob);
                                        attrs.put(type);
                                        attrs.put(manage);
                                        attrs.put(mes);
                                        attrs.put(machinery);
                                        attrs.put(objectclass);
                                        //以下是扩展
                                        attrs.put(addr);
                                        attrs.put(post);
                                        attrs.put(tel);
                                        attrs.put(sr);
                                        attrs.put(nn);
                                        attrs.put(ip);
                                        attrs.put(stuts);
                                        attrs.put(num);
                                       
                                        String AddDn = "uid=" + user + "," + machineryPath;
                                        dctx.createSubcontext(AddDn, attrs);

                                        //创建User 同时在条目下创建Application//用户所属应用
                                        //Attributes AppAttrs1 = new BasicAttributes(true);
                                        //Attribute AppOu1 = new BasicAttribute("ou");
                                        //AppOu1.add("Power");
                                        Attribute AppObjectclass = new BasicAttribute("objectclass");
                                        AppObjectclass.add("organizationalunit");
                                        //AppAttrs1.put(AppOu1);
                                        //AppAttrs1.put(AppObjectclass);
                                        //String AddAppDn1 = "ou=Power,uid=" + user + "," + machineryPath;
                                        //dctx.createSubcontext(AddAppDn1, AppAttrs1);

                                        //创建User 同时在条目下创建Application//用户所属角色
                                        Attributes AppAttrs = new BasicAttributes(true);
                                        Attribute AppOu = new BasicAttribute("ou");
                                        AppOu.add("Role");
                                        AppAttrs.put(AppOu);
                                        AppAttrs.put(AppObjectclass);
                                        String AddAppDn = "ou=Role,uid=" + user + "," + machineryPath;
                                        dctx.createSubcontext(AddAppDn, AppAttrs);

                                        return true;
                                } //if
                        } catch (NamingException e) {
                                e.printStackTrace();
                                System.out.println("DomainManager.createUser() fail...,"
                                                + e.toString());
                                return false;
                        }

                }
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP