Java操作LDAP,如何删除目录?
时间: 2024-03-05 08:50:01 浏览: 125
要删除LDAP目录,可以使用Java中的javax.naming.directory.DirContext接口提供的方法。具体步骤如下:
1. 创建连接并绑定管理员用户:
```
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://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "adminpassword");
DirContext ctx = new InitialDirContext(env);
```
2. 删除目录:
```
ctx.destroySubcontext("ou=people,dc=example,dc=com");
```
这里的参数是要删除的目录的DN(distinguished name),即要删除的目录在LDAP树上的完整路径。
3. 关闭连接:
```
ctx.close();
```
完整代码示例:
```
import javax.naming.*;
import javax.naming.directory.*;
public class LDAPDeleteExample {
public static void main(String[] args) {
try {
// Set up environment for creating initial context
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://localhost:389");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=example,dc=com");
env.put(Context.SECURITY_CREDENTIALS, "adminpassword");
// Create initial context
DirContext ctx = new InitialDirContext(env);
// Delete directory
ctx.destroySubcontext("ou=people,dc=example,dc=com");
// Close the context when we're done
ctx.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
```
阅读全文
相关推荐
















