首先准备openLDAP , window 版或linux版都可,
重点:
You need to add include schema/java.schema
to your OpenLDAP configuration
否则利用JNDI LDAP provider 无法绑定对象。
1. 绑定对象person 到cn=user2,o=tt,c=cn
import java.io.Serializable;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.directory.DirContext;
public class person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
绑定后可以看到LDAP的内容增加了一下内容
javaSerializedData 等信息。
图片请参照百度网盘
http://pan.baidu.com/s/1mhvQWje
并且可以通过LDAP的JNDI来进行进程间通信
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InvalidNameException;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
public class JNDITest1 {
public static String BASE = "o=tt,c=cn";
/**
* 查找一个节点的内容
*
* @param context
*/
public void find(LdapContext context) {
String baseDN = "o=tt,c=cn";
try {
Attributes attrs = context.getAttributes("uid=sa," + baseDN);
// Attribute ocAttr = attrs.get("objectClass");
// for (int i = 0; i < ocAttr.size(); i++) {
// System.out.println(ocAttr.get(i));
// }
Attribute cnAttr = attrs.get("cn");
System.out.println(cnAttr.get());
Attribute snAttr = attrs.get("sn");
System.out.println(snAttr.get());
} catch (NamingException e) {
e.printStackTrace();
}
}
public static InitialLdapContext getLDAPContext() {
// Set up the environment for creating the initial context
Hashtable<String, Object> env = new Hashtable<String, Object>(11);
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,o=tt,c=cn");
env.put(Context.SECURITY_CREDENTIALS, "secret");
try {
// Create the initial context
InitialLdapContext ctx = new InitialLdapContext(env, null);
return ctx;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws NamingException {
JNDITest1.bindObject();
}
public static person jndiSearch() {
InitialLdapContext ctx = getLDAPContext();
try {
person pers = (person) ctx.lookup("cn=user2,o=tt,c=cn");
return pers;
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void CreateDN() throws NamingException {
InitialLdapContext ctx = getLDAPContext();
String newUserName = "user2";
BasicAttributes attrsbu = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectclass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("organizationalPerson");
objclassSet.add("inetOrgPerson");
attrsbu.put(objclassSet);
attrsbu.put("sn", newUserName);
attrsbu.put("uid", newUserName);
ctx.createSubcontext("cn=" + newUserName+","+BASE, attrsbu);
ctx.close();
}
public static void bindObject() {
try {
person p = new person();
p.setName("jordan");
p.setAge(40);
InitialLdapContext ctx = getLDAPContext();
ctx.rebind("cn=user2,o=tt,c=cn", p);
System.out.println("bind object object success ");
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import javax.naming.NamingException;
public class JNDITest2 {
public static String BASE = "o=tt,c=cn";
public static void main(String[] args) throws NamingException {
person p = JNDITest1.jndiSearch();
System.out.println(p.getName());
System.out.println(p.getAge());
}
}
注:如果例子不能完整运行,留言或参照本博客其他文章