java对openldap执行crud操作
import javax.naming.NamingException;
public interface Ldap {
public void connect() throws NamingException;
public void search() throws NamingException;
public void update() throws NamingException;
public void add() throws NamingException;
public void delete() throws NamingException;
public void close() throws NamingException;
}
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
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.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class LdapImpl implements Ldap {
private DirContext ds;
@Override
public void search() throws NamingException {
System.out.println("Searching...");
SearchControls searchCtls = new SearchControls();
// Specify the search scope
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// specify the LDAP search filter
String searchFilter = "uid=test";
// Specify the Base for the search
//String searchBase = "dc=ibm,dc=com";
String searchBase = "o=tcl,c=cn";
// Specify the attributes to return
String returnedAtts[] = { "cn"};
//可以查找多个属性
//String returnedAtts[] = { "cn","mail","userPassword","sn" };
searchCtls.setReturningAttributes(returnedAtts);
// Search for objects using the filter
NamingEnumeration<SearchResult> entries = ds.search(searchBase,
searchFilter, searchCtls);
// Loop through the search results
while (entries.hasMoreElements()) {
SearchResult entry = entries.next();
System.out.println(">>>" + entry.getName());
// Print out the groups
Attributes attrs = entry.getAttributes();
if (attrs != null) {
for (NamingEnumeration<? extends Attribute> names = attrs
.getAll(); names.hasMore();) {
Attribute attr = names.next();
System.out.println("AttributeID: " + attr.getID());
for (NamingEnumeration<?> e = attr.getAll(); e.hasMore();) {
System.out.println("Attributes:" + e.next());
}
}
}
}
System.out.println("Search complete.");
}
@Override
public void update() throws NamingException {
System.out.println("Updating...");
ModificationItem[] mods = new ModificationItem[1];
Attribute attr = new BasicAttribute("cn", "changed value");
// Support add, replace and remove an attribute.
mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);
// ds.modifyAttributes("uid=test,ou=tester,dc=ibm,dc=com", mods);
ds.modifyAttributes("uid=test, o=tcl,c=cn", mods);
System.out.println("Updated.");
}
@Override
public void add() throws NamingException {
System.out.println("Adding...");
Attributes attrs = new BasicAttributes();
attrs.put("uid", "test");
attrs.put("sn", "test");
attrs.put("cn", "test test");
attrs.put("userPassword", "111111".getBytes());
attrs.put("mail", "test@126.com");
attrs.put("labeledURI", "http://unmi.blogcn.com");
// the following attribute has two values
Attribute objclass = new BasicAttribute("objectClass");
objclass.add("inetOrgPerson");
attrs.put(objclass);
//this.ds.createSubcontext("uid=test,ou=tester,dc=ibm,dc=com", attrs);
this.ds.createSubcontext("uid=test, o=tcl,c=cn", attrs);
System.out.println("Add complete.");
}
@Override
public void delete() throws NamingException {
System.out.println("Deleting...");
//this.ds.destroySubcontext("uid=test,ou=tester,dc=ibm,dc=com");
this.ds.destroySubcontext("uid=test,o=tcl,c=cn");
System.out.println("Deleted.");
}
@Override
public synchronized void connect() throws NamingException {
System.out.println("connecting...");
if (ds == null) {
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,dc=ibm,dc=com");
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,o=tcl,c=cn" );
env.put(Context.SECURITY_CREDENTIALS, "secret");
ds = new InitialDirContext(env);
// ds = (DirContext) initial.lookup("ldap://localhost:389");
}
System.out.println("connected.");
}
@Override
public void close() throws NamingException {
System.out.println("closing...");
ds.close();
System.out.println("closed.");
}
}
public class Factory {
private static Ldap instance;
public synchronized static Ldap createInstance() {
if (instance == null) {
try {
instance = (Ldap) Class.forName("com.leech.ldap.LdapImpl").newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return instance;
}
}
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class LDAPTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
test2();
} catch (NamingException e) {
e.printStackTrace();
}
}
public static void test1(){
LDAPTest LDAPTest1 = new LDAPTest();
String root = "o=tcl,c=cn" ; //root
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory" );
env.put(Context.PROVIDER_URL, "ldap://localhost/" + root);
env.put(Context.SECURITY_AUTHENTICATION, "simple" );
env.put(Context.SECURITY_PRINCIPAL, "cn=Manager,o=tcl,c=cn" );
env.put(Context.SECURITY_CREDENTIALS, "secret" );
DirContext ctx = null ;
try {
ctx = new InitialDirContext(env);
System.out.println( "认证成功" );
}
catch (javax.naming.AuthenticationException e) {
e.printStackTrace();
System.out.println( "认证失败" );
}
catch (Exception e) {
System.out.println( "认证出错:" );
e.printStackTrace();
}
if (ctx != null ) {
try {
ctx.close();
}
catch (NamingException e) {
//ignore
}
}
}
public static void test2()throws NamingException {
Ldap ldap = Factory.createInstance();
ldap.connect();
try {
// add uid=test,ou=tester,dc=ibm,dc=com
ldap.add();
// search uid=test
//ldap.search();
// update cn with new value of "changed name"
//ldap.update();
// search uid=test to see cn value.
//ldap.search();
// delete uid=test,ou=tester,dc=ibm,dc=com
//ldap.delete();
// search again.
ldap.search();
} finally {
ldap.close();
}
}
}
相关推荐
java代码操作linux上的openldap目录服务器,进行增删改查操作
对openldap进行增加、删除、修改、查询、用户进行密码验证
openldap浏览器的Java代码实现
windows下openldap的安装与java操作测试 windows下openldap的安装与java操作测试
基于OpenLdap操作工具类(增删改查)
openldap ,这个压缩包十分完整,带浏览器
openldap安装配置 java操作ldap
windows版的openldap,适合于本地开发测试用
ldap安装配置 原理说明 操作应用文档
提供对活动目录操作的一个基础类封装。 OpenLDAP windows 活动目录
db-4.7.25.tar.gz freetype-2.3.5.tar.gz ft254.zip gd-2.0.35.tar.gz gettext-0.16.1.tar.gz httpd-2.4.10.tar.gz jpegsrc.v6b.tar.gz ...包含上述安装包,都是源码包,通过编译安装,在CentOs5.0下运行通过
可以供参考的用于对openldap操作的一系列函数,提供了接口函数.zip
可以供参考的用于对openldap操作的一系列函数,提供了接口函数.rar
java连接LDAP实例和jar包,里面包含ssl连接和更改LDAP属性的例子,公参考
具体的JNDI对openLDAP的操作,java对ldapd增删改查
内涵openldap详细功能讲解,以及openldap的实际操作讲解。
This document is meant to be used in conjunction with other OpenLDAP information resources provided with the software package and on the project's site (http://www.OpenLDAP.org/) on the World Wide Web...
OpenLDAP文档.pptx
主讲OpenLdap安装及配置,OpenLdap是配置ldap的服务器。
openldap-2.5.4