First, Introduction
Lightweight Directory Access Protocol (LDAP), the Lightweight Directory protocol is a protocol to access the online directory service. The following example briefly describes the additions and deletions of the Java Squadron LDAP to the search function. The directory structure is:
Cd=cas,dc=mydc
--cn=users
----Uid=zhangsan
Second, the example
1. Connect LDAP via Ldapcontext
Copy Code code as follows:
/**
* Connect LDAP
*/
@SuppressWarnings ({"Rawtypes", "Unchecked"})
Public Ldapcontext Connetldap () throws Namingexception {
Information needed to connect to LDAP
String ldapfactory = "Com.sun.jndi.ldap.LdapCtxFactory";
String Ldapurl = "Ldap:/ip:port";//URL
String ldapaccount = "Cn=root"; User name
String ldappwd = "password";//Password
Hashtable env = new Hashtable ();
Env.put (Context.initial_context_factory, ldapfactory);
LDAP Server
Env.put (Context.provider_url, Ldapurl);
Env.put (Context.security_authentication, "simple");
Env.put (Context.security_principal, Ldapaccount);
Env.put (Context.security_credentials, ldappwd);
Env.put ("Java.naming.referral", "Follow");
Ldapcontext Ctxtds = new Initialldapcontext (env, NULL);
return Ctxtds;
}
2, Increase user Zhangsan
Copy Code code as follows:
Add to
public void Testadd () throws Exception {
Ldapcontext CTX = Connetldap ();
Attributes attrs = new Basicattributes (true);
Attribute objclass = new BasicAttribute ("objectclass");
Add objectclass
String[] Attrobjectclassperson = {"InetOrgPerson", "Organizationalperson", "person", "top"};
Arrays.sort (Attrobjectclassperson);
for (String Ocp:attrobjectclassperson) {
Objclass.add (OCP);
}
Attrs.put (objclass);
String uid = "Zhangsan";
String UserDN = "uid=" + uid + "," + "CN=USERS,DC=CAS,DC=MYDC";
Password handling
Attrs.put ("UID", UID);
Attrs.put ("cn", UID);
Attrs.put ("SN", UID);
Attrs.put ("DisplayName", "John");
Attrs.put ("Mail", "abc@163.com");
Attrs.put ("description", "");
Attrs.put ("UserPassword", "Passw0rd". GetBytes ("UTF-8"));
Ctx.createsubcontext (UserDN, attrs);
}
3. Delete User Zhangsan
Copy Code code as follows:
Delete
public void Testremove () throws Exception {
Ldapcontext CTX = Connetldap ();
String uid = "Zhangsan";
String UserDN = "uid=" + uid + "," + "CN=USERS,DC=CAS,DC=MYDC";
Ctx.destroysubcontext (USERDN);
}
4, modify the Zhangsan email address
Copy Code code as follows:
Modify
public Boolean testmodify () throws Exception {
Boolean result = true;
Ldapcontext CTX = Connetldap ();
String uid = "Zhangsan";
String UserDN = "uid=" + uid + "," + "CN=USERS,DC=CAS,DC=MYDC";
Attributes attrs = new Basicattributes (true);
Attrs.put ("Mail", "zhangsan@163.com");
Ctx.modifyattributes (UserDN, Dircontext.replace_attribute, attrs);
return result;
}
5. Find Users
Copy Code code as follows:
Inquire
public void Testsearch () throws Exception {
Ldapcontext CTX = Connetldap ();
Setting filter conditions
String uid = "Zhangsan";
String filter = "(& (Objectclass=top) (Objectclass=organizationalperson) (uid= + uid +))";
Restricting the contents of a field to query
String[] Attrpersonarray = {"UID", "UserPassword", "DisplayName", "cn", "SN", "Mail", "description"};
Searchcontrols searchcontrols = new Searchcontrols ();
Searchcontrols.setsearchscope (Searchcontrols.subtree_scope);
Set the attribute to be returned
Searchcontrols.setreturningattributes (Attrpersonarray);
Three parameters are:
Context
The property to search for, if NULL or NULL, all objects in the target context are returned;
Controls the search control and, if NULL, uses the default search control
Namingenumeration<searchresult> answer = Ctx.search ("Cn=users,dc=cas,dc=mydc", filter.tostring (), Searchcontrols);
Output the data that was found
while (Answer.hasmore ()) {
SearchResult result = Answer.next ();
namingenumeration<? Extends attribute> attrs = Result.getattributes (). GetAll ();
while (Attrs.hasmore ()) {
Attribute attr = Attrs.next ();
System.out.println (Attr.getid () + "=" + Attr.get ());
}
System.out.println ("============");
}
}