SoFunction
Updated on 2025-03-06

JAVA_Basic LDAP operation example

1. Introduction

Lightweight Directory Access Protocol (LDAP), a Lightweight Directory Access Protocol is a protocol for accessing online directory services. The following example briefly introduces the addition and deletion function of ldap in the Java squadron. The directory structure is:

CD=CAS,DC=MYDC

--cn=users

----uid=zhangsan

2. Example
1. Connect ldap through LdapContext

Copy the codeThe code is as follows:

/**
* Connect to LDAP
 */ 
@SuppressWarnings({ "rawtypes", "unchecked" }) 
public LdapContext connetLDAP() throws NamingException { 
// Information required to connect to Ldap
    String ldapFactory = ""; 
    String ldapUrl = "ldap:/IP:port";// url 
String ldapAccount = "cn=root"; // Username
String ldapPwd = "password";//Password
    Hashtable env = new Hashtable(); 
    (Context.INITIAL_CONTEXT_FACTORY, ldapFactory); 
    // LDAP server 
    (Context.PROVIDER_URL, ldapUrl); 
    (Context.SECURITY_AUTHENTICATION, "simple"); 
    (Context.SECURITY_PRINCIPAL, ldapAccount); 
    (Context.SECURITY_CREDENTIALS, ldapPwd); 
    ("", "follow"); 
    LdapContext ctxTDS = new InitialLdapContext(env, null); 
    return ctxTDS; 


2. Add users
Copy the codeThe code is 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" }; 
        (attrObjectClassPerson); 
        for (String ocp : attrObjectClassPerson) { 
            (ocp); 
        } 
        (objclass); 
        String uid = "zhangsan"; 
        String userDN = "u," + "cn=users,dc=cas,dc=mydc"; 
// Password processing
        // ("uid", uid); 
        ("cn", uid); 
        ("sn", uid); 
("displayName", "Zhang San");
        ("mail", "abc@"); 
        ("description", ""); 
        ("userPassword", "Passw0rd".getBytes("UTF-8")); 
        (userDN, attrs); 
    } 

3. Delete user zhangsan

Copy the codeThe code is as follows:

//delete
    public void testRemove() throws Exception { 
        LdapContext ctx = connetLDAP(); 
        String uid = "zhangsan"; 
        String userDN = "u," + "cn=users,dc=cas,dc=mydc"; 
        (userDN); 

    } 

4. Modify zhangsan's email address

Copy the codeThe code is as follows:
 
//Revise
    public boolean testModify() throws Exception { 
        boolean result = true; 
        LdapContext ctx = connetLDAP(); 
        String uid = "zhangsan"; 
        String userDN = "u," + "cn=users,dc=cas,dc=mydc"; 
        Attributes attrs = new BasicAttributes(true); 
        ("mail", "zhangsan@"); 
        (userDN, DirContext.REPLACE_ATTRIBUTE, attrs); 
        return result; 

    } 

5. Find users
Copy the codeThe code is as follows:

//Query
    public void testSearch() throws Exception { 
        LdapContext ctx = connetLDAP(); 
// Set filter conditions
        String uid = "zhangsan"; 
        String filter = "(&(objectClass=top)(objectClass=organizationalPerson)(u))"; 
// Limit the content of the field to be queried
        String[] attrPersonArray = { "uid", "userPassword", "displayName", "cn", "sn", "mail", "description" }; 
        SearchControls searchControls = new SearchControls(); 
        (SearchControls.SUBTREE_SCOPE); 
// Set the Attribute to be returned
        (attrPersonArray); 
// The three parameters are:
// Context;
// The attribute to search, if empty or null, returns all objects in the target context;
// The search control that controls the search. If null, the default search control is used.
        NamingEnumeration<SearchResult> answer = ("cn=users,dc=cas,dc=mydc", (), searchControls); 
// Output the found data
        while (()) { 
            SearchResult result = (); 
            NamingEnumeration<? extends Attribute> attrs = ().getAll(); 
            while (()) { 
                Attribute attr = (); 
                (() + "=" + ()); 
            } 
            ("============"); 
        } 
    }