本文目录一览:
- 1、java 怎么样实现新建一个ldap OU,通过用户在控制台自定义输入DN,在AD中自动建好OU相关目录?
- 2、如何使用Java操作LDAP之LDAP连接
- 3、如何通过java操作ldap实现登录
java 怎么样实现新建一个ldap OU,通过用户在控制台自定义输入DN,在AD中自动建好OU相关目录?
写代码程序把你写的这个 DN,分拆成每个 SubContext ,然后在代码中绑定上去:
// initialize config
Properties config = new Properties();
config.setProperty(Context.PROVIDER_URL, 你的 root URL);
Context root = new InititalContext().lookup("");
Context abc = root.createSubContext("ou=Abc");
Context cde = abc.createSubContext("ou=Cde");
...
你的收到的 DN 分拆成这几个步骤依次 createSubContext 就好了。
如何使用Java操作LDAP之LDAP连接
public static void main(String[] args) {
String url = "ldap://10.0.0.10:389/";
String domain = "dc=dtas,dc=com";
String user = "cn=administrator,cn=users";
String password = "111111";
HashtableString, String env = new HashtableString, String();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); // LDAP 工厂
env.put(Context.SECURITY_AUTHENTICATION, "simple"); // LDAP访问安全级别
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_PRINCIPAL, user+","+domain); // 填DN
env.put(Context.SECURITY_CREDENTIALS, password); // AD Password
env.put("java.naming.ldap.attributes.binary", "objectSid objectGUID");
LdapContext ldapCtx = null;
try {
ldapCtx = new InitialLdapContext(env , null);
queryGroup(ldapCtx);
//queryUser(ldapCtx);
} catch (NamingException e) {
e.printStackTrace();
} finally {
if(ldapCtx != null) {
try {
ldapCtx.close();
} catch (NamingException e) {
}
}
}
}
private static void queryGroup(LdapContext ldapCtx) throws NamingException {
SearchControls searchCtls = new SearchControls();
searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
String searchFilter = "objectClass=organizationalUnit";
String searchBase = "ou=myDeptSubDept,ou=myDept,dc=DS-66,dc=com";
String returnedAtts[] = {"distinguishedName", "objectGUID", "name"};
searchCtls.setReturningAttributes(returnedAtts);
NamingEnumerationSearchResult answer = ldapCtx.search(searchBase, searchFilter, searchCtls);
while (answer.hasMoreElements()) {
SearchResult sr = answer.next();
Attributes Attrs = sr.getAttributes();
if (Attrs != null) {
NamingEnumeration? ne = Attrs.getAll();
while(ne.hasMore()) {
Attribute Attr = (Attribute)ne.next();
String name = Attr.getID();
Enumeration? values = Attr.getAll();
if (values != null) { // 迭代
while (values.hasMoreElements()) {
String value = "";
if("objectGUID".equals(name)) {
value = UUID.nameUUIDFromBytes((byte[]) values.nextElement()).toString();
} else {
value = (String)values.nextElement();
}
System.out.println(name + " " + value);
}
}
}
System.out.println("=====================");
}
}
}
如何通过java操作ldap实现登录
catch (NamingException e) {
e.printStackTrace();
} finally {
if(ldapCtx != null) {
try {
ldapCtx.close();
} catch (NamingException e) {
}
}