java实现LDAP数据的增删改查

##java实现LDAP数据的增删改查

一、ldap在很多系统中被使用,再具体的业务中需要写代码来实现ldap的数据管理。首先需要加入ldap的依赖包:

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

二、ldap的数据是层级的树状结构,底层的叶子结点创建需要建立在其父节点之前构造,例如下面的三层ldap entry

第一层:
dn: dc=honor,dc=zhe,dc=wang
objectClass: top
objectClass: domain

第二层:
dn: ou=daye,dc=honor,dc=zhe,dc=wang
objectClass: organizationalUnit
objectClass: top
ou: users

第三层:
dn: cn=houzi,ou=daye,dc=honor,dc=zhe,dc=wang
objectClass: person
objectClass: top
cn: houzi
sn: houzi

三、废话不多说,直接上代码



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");

        // 用户名:cn=Manager,dc=honor,dc=zhe,dc=wang
        // 密码: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);


        //创建第二层:(第一层数据一般是初始化的)
        createSecondEntry(template,"daye");

        //创建第三层:(第一层数据一般是初始化的,第二层需要创建好)
        createThirdEntry(template,"daye","houzi");

    }

    /**
     * 构造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方法即是创建;BasicAttribute 是基本属性,有了类属性之后,才能添加具体的属性
     * @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);

    }

    /**
     * 属性top,person,posixAccount决定了下面的属性:cn,sn,uid,gidNumber等
     * @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);

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值