package com.zt.ad_oper;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.ldap.LdapName;
import java.util.*;
/**
* @author zt 获取AD域用户相关信息
*/
public class LdapOperUtil {
private static LdapOperUtil ldapOperUtil;
private String root = "OU=xxxx,DC=xxxxxxxxxx,DC=xx"; // LDAP的根节点的DC
// private LdapContext ldapContext = null;
private DirContext ldapContext = null;
private LdapOperUtil() {
getConnect();
}
public static LdapOperUtil getNewInstance() {
if (ldapOperUtil == null) {
ldapOperUtil = new LdapOperUtil();
}
return ldapOperUtil;
}
/**
* 连接AD域
*
* @return
*/
private void getConnect() {
Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
String ldapURL = "ldap://" + "192.168.1.xxx" + ":" + 389;
env.put(Context.PROVIDER_URL, ldapURL);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "username");
env.put(Context.SECURITY_CREDENTIALS, "pwd"); //密码
try {
this.ldapContext = new InitialDirContext(env);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 遍历AD域
*
* @throws NamingException
*/
private void getUserInfo() throws NamingException {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
// 注意OU和DC的先后顺序
NamingEnumeration results = this.ldapContext.search("OU=4#608,DC=cipherchina,DC=com", "objectClass=User", searchCtls);
while (results.hasMoreElements()) {
SearchResult sr = (SearchResult) results.next();
Attributes attributes = sr.getAttributes();
NamingEnumeration values = attributes.getAll();
while (values.hasMore()) {
Attribute attr = (Attribute) values.next();
Enumeration vals = attr.getAll();
while (vals.hasMoreElements()) {
Object o = vals.nextElement();
System.out.println(attr.getID() + "--------------" + o.toString());
}
}
}
}
/**
* 添加一个用户
* @param newUserName
*/
private void add(String newUserName) {
try {
BasicAttributes attrs = new BasicAttributes();
BasicAttribute objclassSet = new BasicAttribute("objectClass");
objclassSet.add("person");
objclassSet.add("top");
objclassSet.add("user");
attrs.put(objclassSet);
attrs.put("ou", newUserName);
this.ldapContext.createSubcontext("CN=" + newUserName + ",ou=java," + root, attrs);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除一个用户
* @param dn
*/
private void delete(String dn) {
try {
this.ldapContext.destroySubcontext(dn);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 重命名一个用户
* @param oldDN
* @param newDN
* @return
*/
public boolean renameEntry(String oldDN, String newDN) {
try {
this.ldapContext.rename(oldDN, newDN);
return true;
} catch (NamingException ne) {
ne.printStackTrace();
return false;
}
}
public static void main(String[] args) throws NamingException {
LdapOperUtil ldap = LdapOperUtil.getNewInstance();
try {
// ldap.getUserInfo();
// 删除一个用户
// ldap.delete("CN=test,OU=java,"+ldap.root);
// 添加一个用户
// ldap.add("test");
// 重命名一个用户
// ldap.renameEntry("cn=test1,"+ldap.root,"cn=xx,ou=java,"+ldap.root);
} catch (Exception e) {
e.printStackTrace();
}
// ldap.getUserInfo();
}
}
java操作AD域增删改查
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- hadoop-HDFS除了在linux上以shell的方式进行操作外,还可以利用java来操作,接下来我们就来实现...
- 准备 1.通过npm命令安装mongodb2.安装mongodb数据库,这里不详细介绍了,网上一堆 CRUD操作 ...