有可以动态创建LDAP schema的JAVA api吗? 或可以在schema中动态增加 objectclass 或者 attributetype 吗? 有可以动态创建LDAP schema的JAVA api吗?或可以在schema中动态增加 objectclass 或者 attributetype 吗?请高手指点!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 两年前做过一点OpenLDAP的东西,楼主说的应该可以实现,给你推荐一个我当时学习时的一个地方:http://blog.csdn.net/njchenyi/article/details/2051293这位博主的资料挺好的。 首先感谢这位有心人。那位博主的资料例子写的是从已有的schema定义中获取信息,我想:用JAVA代码生成SCHEMA(java创建SCHEMA文件,添加objectClass和Attribute、Attribute的类型、描述、匹配规则等,如:EQUALITY numericStringMatch SUBSTR numericStringSubstringsMatch)?不知道有实例吗? 今天在调代码,突然发现以前的代码,给你贴上吧:import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.DataOutputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;import java.util.Hashtable;import javax.naming.directory.*;import java.util.*;import javax.naming.*;import javax.naming.directory.BasicAttribute;import javax.naming.directory.BasicAttributes;import javax.naming.directory.DirContext;import javax.naming.directory.InitialDirContext;import javax.naming.directory.ModificationItem;import org.bouncycastle.util.encoders.Base64;public class OpenLDAPTest1 extends Thread{ DirContext dc = null; String account = "test";//操作LDAP的帐户。默认就是Manager。 String password = "secret";//帐户Manager的密码。 String root = "o=datech,c=cn"; //LDAP的根节点的DC public OpenLDAPTest1() { init(); //add();//添加节点 //delete("ou=hi,dc=example,dc=com");//删除"ou=hi,dc=example,dc=com"节点 //modifyInformation("ou=hi,dc=example,dc=com");//修改"ou=hi,dc=example,dc=com"属性 //重命名节点"ou=new,o=neworganization,dc=example,dc=com" //renameEntry("ou=new,o=neworganization,dc=example,dc=com", //"ou=neworganizationalUnit,o=neworganization,dc=example,dc=com"); //searchInformation("o=tcl,c=cn", "", "(objectclass=*)");//遍历所有根节点 //遍历指定节点的分节点 //searchInformation("o=neworganization,dc=example,dc=com","","(objectclass=*)"); //close(); } public void init() { if (dc != null) { try { dc.close(); } catch (NamingException e) { System.out.println("NamingException in close():" + e); } } Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://192.168.0.85:389/"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root); env.put(Context.SECURITY_CREDENTIALS, password); try { dc = new InitialDirContext(env);//初始化上下文 System.out.println("认证成功");//这里可以改成异常抛出。 } catch (javax.naming.AuthenticationException e) { System.out.println("认证失败"); } catch (Exception e) { System.out.println("认证出错:" + e); } } public void close() { if (dc != null) { try { dc.close(); } catch (NamingException e) { System.out.println("NamingException in close():" + e); } } } public void add() { try { String newUserName = "hi"; BasicAttributes attrs = new BasicAttributes(); BasicAttribute objclassSet = new BasicAttribute("objectClass"); objclassSet.add("top"); objclassSet.add("organizationalUnit"); attrs.put(objclassSet); attrs.put("ou", newUserName); dc.createSubcontext("ou=" + newUserName + "," + root, attrs); } catch (Exception e) { e.printStackTrace(); System.out.println("Exception in add():" + e); } } public void delete(String dn) { try { dc.destroySubcontext(dn); } catch (Exception e) { e.printStackTrace(); System.out.println("Exception in delete():" + e); } } public boolean modifyInformation(String dn) { try { ModificationItem[] mods = new ModificationItem[1]; /*添加属性*/// Attribute attr0 = new BasicAttribute("description",// "测试");// mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0); /*修改属性*/// Attribute attr0 = new BasicAttribute("description", "尚");// mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,// attr0); /*删除属性*/ Attribute attr0 = new BasicAttribute("description", "尚"); mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, attr0); dc.modifyAttributes(dn, mods); return true; } catch (NamingException ne) { ne.printStackTrace(); System.err.println("Error: " + ne.getMessage()); return false; } } /** * @param base :根节点(在这里是"o=tcl,c=cn") * @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历) * @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点) */ public void searchInformation(String base, String scope, String filter) { SearchControls sc = new SearchControls(); if (scope.equals("base")) { sc.setSearchScope(SearchControls.OBJECT_SCOPE); } else if (scope.equals("one")) { sc.setSearchScope(SearchControls.ONELEVEL_SCOPE); } else { sc.setSearchScope(SearchControls.SUBTREE_SCOPE); } NamingEnumeration ne = null; try { ne = dc.search(base, filter, sc); // Use the NamingEnumeration object to cycle through // the result set. while (ne.hasMore()) { System.out.println(); SearchResult sr = (SearchResult) ne.next(); String name = sr.getName();//System.out.println("************name = sr.getName(): " + name); if (base != null && !base.equals("")) { System.out.println("entry: " + name + "," + base); } else { System.out.println("entry: " + name); } Attributes at = sr.getAttributes(); NamingEnumeration ane = at.getAll(); while (ane.hasMore()) { Attribute attr = (Attribute) ane.next(); String attrType = attr.getID();System.out.println("********attrType = attr.getID(): " + attrType); NamingEnumeration values = attr.getAll(); Vector vals = new Vector(); // Another NamingEnumeration object, this time // to iterate through attribute values. while (values.hasMore()) { Object oneVal = values.nextElement(); if (oneVal instanceof String) { System.out.println(attrType + ": " + (String) oneVal); } else { System.out.println(attrType + ": " + new String((byte[]) oneVal)); } } if(values != null){ values.close(); } } if(ane != null){ ane.close(); } } if(ne != null){ ne.close(); } } catch (Exception nex) { System.err.println("Error: " + nex.getMessage()); nex.printStackTrace(); } } public void searchOneNode(String base){ SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.OBJECT_SCOPE); String filter = "(objectclass=*)"; NamingEnumeration ne = null; try { ne = dc.search(base, filter, sc); while(ne.hasMore()){ System.out.println(); SearchResult sr = (SearchResult) ne.next(); String name = sr.getName(); if(base != null && !base.equals("")){ System.out.println("entry: " + name + "," + base); } else { System.out.println("entry: " + name); } Attributes at = sr.getAttributes(); NamingEnumeration ane = at.getAll(); while(ane.hasMore()){ Attribute attr = (Attribute) ane.next(); String attrType = attr.getID(); NamingEnumeration values = attr.getAll(); Vector vals = new Vector(); while(values.hasMore()){ Object oneVal = values.next(); if(oneVal instanceof String){ System.out.println(attrType + ":" + oneVal); }else{ System.out.println(attrType + ":" + new String((byte[]) oneVal)); } } if(values != null){ values.close(); } } if(ane != null){ ane.close(); } } if(ne != null){ ne.close(); } } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void searchAttrOfValue(String base, String attribute){ SearchControls sc = new SearchControls(); sc.setSearchScope(SearchControls.OBJECT_SCOPE); String filter = "(objectclass=*)"; String outValue = null; NamingEnumeration ne = null; try { ne = dc.search(base, filter, sc); while(ne.hasMore()){ System.out.println(); SearchResult sr = (SearchResult) ne.next(); String name = sr.getName(); if(base != null && !base.equals("")){ System.out.println("entry: " + name + "," + base); } else { System.out.println("entry: " + name); } Attributes at = sr.getAttributes(); NamingEnumeration ane = at.getAll(); while(ane.hasMore()){ Attribute attr = (Attribute) ane.next(); String attrType = attr.getID(); if(attrType.equals(attribute)){ NamingEnumeration values = attr.getAll(); Vector vals = new Vector(); int num = 1; while(values.hasMore()){ Object oneVal = values.next(); if(oneVal instanceof String){ System.out.println(attrType + "-------:" + oneVal); outValue = attrType + ": " + oneVal; }else{ System.out.println(attrType + "+++++++:" + new String(Base64.encode((byte[]) oneVal)));// Base64.encode((byte[]) oneVal);// outValue = attrType + ": " + new String((byte[]) oneVal); outValue = new String(Base64.encode((byte[]) oneVal)); fileStore("outFile" + num , outValue); num ++; } } if(values != null){ values.close(); } } } if(ane != null){ ane.close(); } } if(ne != null){ ne.close(); } } catch (NamingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //********************// fileStore("outFile", outValue); } public boolean renameEntry(String oldDN, String newDN) { try { dc.rename(oldDN, newDN); return true; } catch (NamingException ne) { System.err.println("Error: " + ne.getMessage()); return false; } } public void fileStore(String outFile, String attrValue){ String str = null; BufferedReader in = null; PrintWriter out = null; try { in = new BufferedReader(new StringReader(attrValue)); out = new PrintWriter( new BufferedWriter( new FileWriter(outFile + ".crl", false))); while((str=in.readLine()) != null){// out.println(new Date()); out.println(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(in != null){ in.close(); in = null; } if(out != null){ out.close(); out = null; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* private class LdapThread extends Thread{ public void run(){ try { while (true) { searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail"); this.sleep(60000); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }*/ public void run(){ try { while (true) { searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail"); this.sleep(60000); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} /* public static void main(String[] args) { OpenLDAPTest1 ldap = new OpenLDAPTest1();// ldap.searchInformation("o=tcl,c=cn", "", "(objectclass=*)");// ldap.searchOneNode("uid=aaa,o=tcl,c=cn");// ldap.searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");// new LdapThread().start(); ldap.start(); ldap.close(); }*/} 楼主您好,请问您的问题解决了吗,我也遇到了相同的需求,需要通过java修改LDAP的SCHEMA。如果您解决了,可不可以把解决方法赐教一下。[email protected]不胜感激涕零... 100分!求解决Struts2 配置问题 如何强制Text文本框为大写字母 各位大虾帮我看看怎么解决这个问题啊??? struts中 download之后画面关闭的问题--急急。。。有劳达人指点 求助:急急急!!!!tomcat如何对ie浏览器进行设置 求问JSP中的 一个很简单的问题,急!在线等! 请问在表单上把一个select设置为readonly然后在request中就取不到值了。 jsp开发基于unix平台的网站程序 如何配置jsp环境在windows98下? Javaweb数据权限问题 求大神 关于js返回前一页面的问题 系统发布到服务器,两台电脑访问页面显示不同
给你推荐一个我当时学习时的一个地方:
http://blog.csdn.net/njchenyi/article/details/2051293
这位博主的资料挺好的。
首先感谢这位有心人。
那位博主的资料例子写的是从已有的schema定义中获取信息,我想:
用JAVA代码生成SCHEMA(java创建SCHEMA文件,添加objectClass和Attribute、Attribute的类型、描述、匹配规则等,如:
EQUALITY numericStringMatch SUBSTR numericStringSubstringsMatch)?
不知道有实例吗?
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Hashtable;
import javax.naming.directory.*;import java.util.*;
import javax.naming.*;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;import org.bouncycastle.util.encoders.Base64;public class OpenLDAPTest1 extends Thread{ DirContext dc = null;
String account = "test";//操作LDAP的帐户。默认就是Manager。
String password = "secret";//帐户Manager的密码。
String root = "o=datech,c=cn"; //LDAP的根节点的DC public OpenLDAPTest1() {
init();
//add();//添加节点
//delete("ou=hi,dc=example,dc=com");//删除"ou=hi,dc=example,dc=com"节点
//modifyInformation("ou=hi,dc=example,dc=com");//修改"ou=hi,dc=example,dc=com"属性
//重命名节点"ou=new,o=neworganization,dc=example,dc=com"
//renameEntry("ou=new,o=neworganization,dc=example,dc=com",
//"ou=neworganizationalUnit,o=neworganization,dc=example,dc=com");
//searchInformation("o=tcl,c=cn", "", "(objectclass=*)");//遍历所有根节点
//遍历指定节点的分节点
//searchInformation("o=neworganization,dc=example,dc=com","","(objectclass=*)");
//close();
} public void init() {
if (dc != null) {
try {
dc.close();
} catch (NamingException e) {
System.out.println("NamingException in close():" + e);
}
}
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://192.168.0.85:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=" + account + "," + root);
env.put(Context.SECURITY_CREDENTIALS, password);
try {
dc = new InitialDirContext(env);//初始化上下文
System.out.println("认证成功");//这里可以改成异常抛出。
} catch (javax.naming.AuthenticationException e) {
System.out.println("认证失败");
} catch (Exception e) {
System.out.println("认证出错:" + e);
}
} public void close() {
if (dc != null) {
try {
dc.close();
} catch (NamingException e) {
System.out.println("NamingException in close():" + e);
}
}
} public void add() {
try {
String newUserName = "hi";
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectClass");
objclassSet.add("top");
objclassSet.add("organizationalUnit");
attrs.put(objclassSet);
attrs.put("ou", newUserName);
dc.createSubcontext("ou=" + newUserName + "," + root, attrs);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in add():" + e);
}
} public void delete(String dn) {
try {
dc.destroySubcontext(dn);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Exception in delete():" + e);
}
}
try {
ModificationItem[] mods = new ModificationItem[1]; /*添加属性*/
// Attribute attr0 = new BasicAttribute("description",
// "测试");
// mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,attr0); /*修改属性*/
// Attribute attr0 = new BasicAttribute("description", "尚");
// mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
// attr0); /*删除属性*/
Attribute attr0 = new BasicAttribute("description", "尚");
mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE,
attr0);
dc.modifyAttributes(dn, mods);
return true;
} catch (NamingException ne) {
ne.printStackTrace();
System.err.println("Error: " + ne.getMessage());
return false;
} } /**
* @param base :根节点(在这里是"o=tcl,c=cn")
* @param scope :搜索范围,分为"base"(本节点),"one"(单层),""(遍历)
* @param filter :指定子节点(格式为"(objectclass=*)",*是指全部,你也可以指定某一特定类型的树节点)
*/
public void searchInformation(String base, String scope, String filter) {
SearchControls sc = new SearchControls();
if (scope.equals("base")) {
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
} else if (scope.equals("one")) {
sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
} else {
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
} NamingEnumeration ne = null;
try {
ne = dc.search(base, filter, sc);
// Use the NamingEnumeration object to cycle through
// the result set.
while (ne.hasMore()) {
System.out.println();
SearchResult sr = (SearchResult) ne.next();
String name = sr.getName();
//System.out.println("************name = sr.getName(): " + name);
if (base != null && !base.equals("")) {
System.out.println("entry: " + name + "," + base);
} else {
System.out.println("entry: " + name);
} Attributes at = sr.getAttributes();
NamingEnumeration ane = at.getAll(); while (ane.hasMore()) {
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
System.out.println("********attrType = attr.getID(): " + attrType);
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
// Another NamingEnumeration object, this time
// to iterate through attribute values.
while (values.hasMore()) {
Object oneVal = values.nextElement();
if (oneVal instanceof String) {
System.out.println(attrType + ": " + (String) oneVal);
} else {
System.out.println(attrType + ": " + new String((byte[]) oneVal));
}
}
if(values != null){
values.close();
}
}
if(ane != null){
ane.close();
}
}
if(ne != null){
ne.close();
}
} catch (Exception nex) {
System.err.println("Error: " + nex.getMessage());
nex.printStackTrace();
}
} public void searchOneNode(String base){
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
String filter = "(objectclass=*)";
NamingEnumeration ne = null;
try {
ne = dc.search(base, filter, sc);
while(ne.hasMore()){
System.out.println();
SearchResult sr = (SearchResult) ne.next();
String name = sr.getName();
if(base != null && !base.equals("")){
System.out.println("entry: " + name + "," + base);
} else {
System.out.println("entry: " + name);
}
Attributes at = sr.getAttributes();
NamingEnumeration ane = at.getAll();
while(ane.hasMore()){
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
while(values.hasMore()){
Object oneVal = values.next();
if(oneVal instanceof String){
System.out.println(attrType + ":" + oneVal);
}else{
System.out.println(attrType + ":" + new String((byte[]) oneVal));
}
}
if(values != null){
values.close();
}
}
if(ane != null){
ane.close();
}
}
if(ne != null){
ne.close();
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void searchAttrOfValue(String base, String attribute){
SearchControls sc = new SearchControls();
sc.setSearchScope(SearchControls.OBJECT_SCOPE);
String filter = "(objectclass=*)";
String outValue = null;
NamingEnumeration ne = null;
try {
ne = dc.search(base, filter, sc);
while(ne.hasMore()){
System.out.println();
SearchResult sr = (SearchResult) ne.next();
String name = sr.getName();
if(base != null && !base.equals("")){
System.out.println("entry: " + name + "," + base);
} else {
System.out.println("entry: " + name);
}
Attributes at = sr.getAttributes();
NamingEnumeration ane = at.getAll();
while(ane.hasMore()){
Attribute attr = (Attribute) ane.next();
String attrType = attr.getID();
if(attrType.equals(attribute)){
NamingEnumeration values = attr.getAll();
Vector vals = new Vector();
int num = 1;
while(values.hasMore()){
Object oneVal = values.next();
if(oneVal instanceof String){
System.out.println(attrType + "-------:" + oneVal);
outValue = attrType + ": " + oneVal;
}else{
System.out.println(attrType + "+++++++:" + new String(Base64.encode((byte[]) oneVal)));
// Base64.encode((byte[]) oneVal);
// outValue = attrType + ": " + new String((byte[]) oneVal);
outValue = new String(Base64.encode((byte[]) oneVal));
fileStore("outFile" + num , outValue);
num ++;
}
}
if(values != null){
values.close();
}
}
}
if(ane != null){
ane.close();
}
}
if(ne != null){
ne.close();
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//********************
// fileStore("outFile", outValue);
}
public boolean renameEntry(String oldDN, String newDN) {
try {
dc.rename(oldDN, newDN);
return true;
} catch (NamingException ne) {
System.err.println("Error: " + ne.getMessage());
return false;
}
} public void fileStore(String outFile, String attrValue){
String str = null;
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new StringReader(attrValue));
out = new PrintWriter(
new BufferedWriter(
new FileWriter(outFile + ".crl", false)));
while((str=in.readLine()) != null){
// out.println(new Date());
out.println(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(in != null){
in.close();
in = null;
}
if(out != null){
out.close();
out = null;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
private class LdapThread extends Thread{
public void run(){
try {
while (true) {
searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");
this.sleep(60000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
public void run(){
try {
while (true) {
searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");
this.sleep(60000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
public static void main(String[] args) {
OpenLDAPTest1 ldap = new OpenLDAPTest1();
// ldap.searchInformation("o=tcl,c=cn", "", "(objectclass=*)");
// ldap.searchOneNode("uid=aaa,o=tcl,c=cn");
// ldap.searchAttrOfValue("uid=Unmi,o=tcl,c=cn", "mail");
// new LdapThread().start();
ldap.start();
ldap.close();
}*/
}
如果您解决了,可不可以把解决方法赐教一下。[email protected]不胜感激涕零...