Add, delete, modify and query LDAP data with java

Keywords: Java Spring Apache Attribute

Add, delete, modify and query LDAP data with java

1, ldap is used in many systems, and code is needed to realize data management of ldap in specific business. First, add the dependency package of ldap:

    <dependency>
        <groupId>org.springframework.ldap</groupId>
        <artifactId>spring-ldap-core</artifactId>
        <version>2.1.0.RELEASE</version>
    </dependency>

2, The data of ldap is a hierarchical tree structure. The creation of the underlying leaf node needs to be constructed before its parent node, for example, the following three-tier ldap entry

First floor:
dn: dc=honor,dc=zhe,dc=wang
objectClass: top
objectClass: domain

//The second floor:
dn: ou=daye,dc=honor,dc=zhe,dc=wang
objectClass: organizationalUnit
objectClass: top
ou: users

//The third level:
dn: cn=houzi,ou=daye,dc=honor,dc=zhe,dc=wang
objectClass: person
objectClass: top
cn: houzi
sn: houzi

3, No more nonsense, just code

package com.chinamobile.cmss.bdoc.ldap;

import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.core.AuthenticationSource;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;

import javax.naming.Name;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import java.util.Random;

/**
 * Created by xhh on 2018/4/1 14:47.
 */
public class LdapDemoTest {
    public static void main(String[] args) {

        LdapContextSource cs = new LdapContextSource();
        cs.setCacheEnvironmentProperties(false);
        cs.setUrl("ldap://192.168.10.26:389");
        cs.setBase("dc=honor,dc=zhe,dc=wang");

        // User name: CN = manager, DC = honor, DC = Zh, DC = Wang
        // Password: 123456
        cs.setAuthenticationSource(new AuthenticationSource() {
            public String getCredentials() {
                return "cn=Manager,dc=honor,dc=zhe,dc=wang";
            }
            public String getPrincipal() {
                return "123456";
            }
        });
        LdapTemplate template = new LdapTemplate(cs);


        //Create the second layer: (the first layer data is generally initialized)
        createSecondEntry(template,"daye");

        //Create the third layer: (the first layer data is generally initialized, and the second layer needs to be created well)
        createThirdEntry(template,"daye","houzi");

    }

    /**
     * Construct dn, Name
     * @param type
     * @param commonName
     * @return
     */
    public static DistinguishedName getDn(String type, String commonName) {
        DistinguishedName dn = new DistinguishedName();
        if (StringUtils.isNotBlank(type)) {
            dn.add("ou", type);
        }
        if (StringUtils.isNotBlank(commonName)) {
            dn.add("cn", commonName);
        }
        return dn;
    }

    /**
     * bind Method is to create; basicatattribute is a basic attribute, and you can add specific attributes only after you have class attributes
     * @param template
     * @param secondName
     */
    public static void createSecondEntry(LdapTemplate template, String secondName){
        Name dn = getDn(secondName,null);
        BasicAttribute baAttr = new BasicAttribute("objectClass");
        baAttr.add("top");
        baAttr.add("organizationalUnit");
        Attributes attrs = new BasicAttributes();
        attrs.put(baAttr);
        attrs.put("ou", secondName);
        template.bind(dn, null, attrs);

    }

    /**
     * The attributes top, person,posixAccount determine the following attributes: cn,sn,uid,gidNumber, etc
     * @param template
     * @param secondName
     * @param thirdName
     */
    public static void createThirdEntry(LdapTemplate template, String secondName, String thirdName){
        Name dn = getDn(secondName,thirdName);
        BasicAttribute baAttr = new BasicAttribute("objectClass");
        baAttr.add("top");
        baAttr.add("person");
        baAttr.add("inetOrgPerson");
        baAttr.add("posixAccount");
        baAttr.add("shadowAccount");
        Attributes attrs = new BasicAttributes();
        attrs.put(baAttr);
        attrs.put("cn", thirdName);
        attrs.put("sn", thirdName);
        Random random = new Random();
        String uidNumber = random.nextInt(2000)+"";
        attrs.put("uid", thirdName);
        attrs.put("gidNumber", uidNumber);
        attrs.put("uidNumber", uidNumber);
        attrs.put("loginShell","/bin/bash");
        template.bind(dn, null, attrs);

    }
}

Posted by Cobby on Thu, 02 Apr 2020 19:26:51 -0700