JAVA操作LDAP目录服务解决方法
JAVA操作LDAP目录服务
最近项目有用到LDAP目录服务,第一次接触这个东西,还请大神们帮帮忙.
这张图是我用Softerra LDAP Browser工具剪切下来的目录图片.我现在想要的效果是:在这多个ou列中新加一个ou=indicate目录,然后再想里面加相关数据,并且这些数据是以一对多的关系存在.
自己写了一段代码,请查看
得到链接之后,就开始进行新增方法
进行测试
执行之后,控制台报错
不知道是不是我写的方法不对,真心不熟悉这个东西,还请各位帮忙提提建议,小弟在此谢过了.
------解决思路----------------------
没用过,网上说对象冲突。
------解决思路----------------------
/**
* 添加用户
*
* @return
*/
public static boolean addUser() {
boolean success = false;
DirContext ctx = null;
try {
String ou = "wenjie2";
String newUserName = "Alex";
ctx = LdapHelper.getCtx();
BasicAttributes attrsbu = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("inetOrgPerson");
attrsbu.put(objclassSet);
attrsbu.put("sn", newUserName);
attrsbu.put("uid", newUserName);
attrsbu.put("cn", newUserName);
attrsbu.put("userPassword", "48925432");
attrsbu.put("mail", "wenjie2@sina.com");
attrsbu.put("telephoneNumber", "137110422792");
ctx.createSubcontext("ou=" + ou + "", attrsbu);
ctx.close();
return true;
} catch (NamingException ex) {
try {
if (ctx != null) {
ctx.close();
}
} catch (NamingException namingException) {
namingException.printStackTrace();
}
Logger.getLogger(LdapHelper.class.getName()).log(Level.SEVERE,
null, ex);
}
return false;
}
最近项目有用到LDAP目录服务,第一次接触这个东西,还请大神们帮帮忙.
这张图是我用Softerra LDAP Browser工具剪切下来的目录图片.我现在想要的效果是:在这多个ou列中新加一个ou=indicate目录,然后再想里面加相关数据,并且这些数据是以一对多的关系存在.
自己写了一段代码,请查看
/**
* Ldap连接
*
* @throws Exception
*/
private static InitialLdapContext LdapConnect() throws Exception {
try {
String url ="ldap://localhost:389";
String name ="cn=root";
String pwd = "root";
System.out.println(url+" "+name+" "+pwd);
Hashtable hashtable = new Hashtable();
hashtable.put(Context.AUTHORITATIVE, "true");
hashtable.put("com.sun.jndi.ldap.connect.pool", "true");
hashtable.put("java.naming.ldap.version", "3");
hashtable.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
hashtable.put(Context.REFERRAL, "follow");
hashtable.put(Context.PROVIDER_URL, url);
hashtable.put(Context.SECURITY_AUTHENTICATION, "Simple");
hashtable.put(Context.SECURITY_PRINCIPAL, name);
hashtable.put(Context.SECURITY_CREDENTIALS, pwd);
ctx = new InitialLdapContext(hashtable, null);
System.out.println("Get LdapConnect:ctx=" + ctx);
} catch (Exception e) {
System.out.println("LdapConnect Sys Error:" + e.getMessage());
throw new Exception(e.getMessage());
}
return ctx;
}
得到链接之后,就开始进行新增方法
/**
* 新增
* @param phone 固话
* @param indicate 标示号
*/
public static void getAdd(String phone,String indicate){
try {
Attributes attrs = new BasicAttributes();
Attribute objectClassAttr = new BasicAttribute("objectclass");
objectClassAttr.add("top");
objectClassAttr.add("person");
objectClassAttr.add("ePerson");
objectClassAttr.add("SHTELIndicate");
attrs.put(objectClassAttr);
attrs.put("phone", phone);
attrs.put("cn", phone);
attrs.put("sn", phone);
attrs.put("indicate", indicate);
LdapConnect().createSubcontext("cn=" + phone+ ",ou=indicate,dc=account,dc=mboss,dc=com", attrs);
System.out.println("cg");
} catch (Exception e) {
e.printStackTrace();
}
}
进行测试
public static void main(String[] args) {
getAdd("33589765","201309041705");
}
执行之后,控制台报错
不知道是不是我写的方法不对,真心不熟悉这个东西,还请各位帮忙提提建议,小弟在此谢过了.
------解决思路----------------------
没用过,网上说对象冲突。
------解决思路----------------------
/**
* 添加用户
*
* @return
*/
public static boolean addUser() {
boolean success = false;
DirContext ctx = null;
try {
String ou = "wenjie2";
String newUserName = "Alex";
ctx = LdapHelper.getCtx();
BasicAttributes attrsbu = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("inetOrgPerson");
attrsbu.put(objclassSet);
attrsbu.put("sn", newUserName);
attrsbu.put("uid", newUserName);
attrsbu.put("cn", newUserName);
attrsbu.put("userPassword", "48925432");
attrsbu.put("mail", "wenjie2@sina.com");
attrsbu.put("telephoneNumber", "137110422792");
ctx.createSubcontext("ou=" + ou + "", attrsbu);
ctx.close();
return true;
} catch (NamingException ex) {
try {
if (ctx != null) {
ctx.close();
}
} catch (NamingException namingException) {
namingException.printStackTrace();
}
Logger.getLogger(LdapHelper.class.getName()).log(Level.SEVERE,
null, ex);
}
return false;
}