java template实现LDAP增删改查

java template实现LDAP增删改查

前言

  • 使用的是apache directory api。
  • 自己搭建了一个LDAP Server
    • 版本2.44
  • 实现了对LDAP服务器的增删改查

添加maven依赖

<dependency>
            <groupId>org.apache.directory.api</groupId>
            <artifactId>api-all</artifactId>
            <version>1.0.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.2</version>
        </dependency>

有不懂得地方欢迎邮件咨询

1、连接LDAP
  • 连接使用的连接池
  • 首先先配置自己的参数
    • ip
    • 端口
    • dn
    • 密码
  • 后面的参数默认就可以了
LdapConnectionConfig config = new LdapConnectionConfig();
        config.setLdapHost( "hostname" );
        config.setLdapPort( 389 );
        config.setName( "yourDn" );
        config.setCredentials( "密码" );

        DefaultLdapConnectionFactory factory = new DefaultLdapConnectionFactory( config );
        factory.setTimeOut( 1000 );


// optional, values below are defaults
        GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
        poolConfig.lifo = true;
        poolConfig.maxActive = 8;
        poolConfig.maxIdle = 8;
        poolConfig.maxWait = -1L;
        poolConfig.minEvictableIdleTimeMillis = 1000L * 60L * 30L;
        poolConfig.minIdle = 0;
        poolConfig.numTestsPerEvictionRun = 3;
        poolConfig.softMinEvictableIdleTimeMillis = -1L;
        poolConfig.testOnBorrow = false;
        poolConfig.testOnReturn = false;
        poolConfig.testWhileIdle = false;
        poolConfig.timeBetweenEvictionRunsMillis = -1L;
        poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;

// could use DefaultPoolableLdapConnectionFactory instead
// see javadoc for ValidatingPoolableLdapConnectionFactory
// for an explanation
        ldapConnectionTemplate =
                new LdapConnectionTemplate( new LdapConnectionPool(
                        new ValidatingPoolableLdapConnectionFactory( factory ), poolConfig ) );

2、新增
  • 在新增的时候需要注意的就是你的dn的位置
  • 后面就是添加你想要的属性的键值对
  • 具体写法如下:
public void add(final StuStudentinfo stu){
        // using RequestBuilder

        AddResponse response = ldapConnectionTemplate.add(
                ldapConnectionTemplate.newDn( "cn="+stu.getId()+",cn=admin,dc=feng,dc=cn" ),
                new RequestBuilder<AddRequest>() {
                    @Override
                    public void buildRequest( AddRequest request ) throws LdapException {
                        request.getEntry()
                                .add( "objectClass", "top", "student" )
                                .add( "cn", stu.getStuSex() )
                                .add( "stuName", stu.getStuName() )
                                .add( "stuNumber", stu.getStuNumber() )
                                .add( "stuPhone", stu.getStuPhone() )
                                .add( "stuAge", stu.getStuAge() )
                                .add( "createBy", String.valueOf(stu.getCreateBy()) )
                                .add( "createDate",  String.valueOf(stu.getCreateDate()))
                                .add( "updateBy",  String.valueOf(stu.getUpdateBy()) )
                                .add( "updateDate",  String.valueOf(stu.getUpdateDate()))
                                        .add( "stuid",  String.valueOf(stu.getId())
                                        );
                    }
                } );
        System.out.println(response);

3、查找
  • 查找是根据dn去找到你的entry然后返回整个entry你可以更具entry取出你想要的attribute
  • 参数
    • dn
    • attribute
    public void search(StuStudentinfo stu){
        // using a previously defined EntryMapper
        Entry rstu = ldapConnectionTemplate.lookup(
                ldapConnectionTemplate.newDn( "cn="+stu.getId()+",cn=admin,dc=feng,dc=cn" ),

        new EntryMapper<Entry>() {
            @Override
            public Entry map( Entry entry ) throws LdapException {
                return entry;
            }
        }
        );
        if(rstu==null) {
            System.out.println(rstu);
            return;
        }


        StuStudentinfo stu1 = new StuStudentinfo();
        stu1.setId(String.valueOf(rstu.get("stuid")));
        stu1.setCreateBy(String.valueOf(rstu.get("createBy")));
        stu1.setStuName(String.valueOf(rstu.get("stuName")));
        stu1.setStuNumber(String.valueOf(rstu.get("stuid")));
        stu1.setStuAge(String.valueOf(rstu.get("stuid").getUpId()));
        stu1.setCreateDate(String.valueOf(rstu.get("createDate")));
        stu1.setUpdateBy(String.valueOf(rstu.get("updateBy")));
        stu1.setUpdateDate(String.valueOf(rstu.get("updateDate").getUpId()));
        stu1.setStuSex(String.valueOf(rstu.get("cn")));
        stu1.setStuPhone(rstu.get("stuPhone").getUpId());


        System.out.println(stu1);

    }
4、删除
  • 根据dn删除具体的entry
public void delete(StuStudentinfo stu){
    // using DN only
    DeleteResponse response = ldapConnectionTemplate.delete(
            ldapConnectionTemplate.newDn("cn="+stu.getId()+",cn=admin,dc=feng,dc=cn" ) );
}
5、修改
  • 根据dn找到具体的entry,修改他的attribute
 public void modify(){
        // using RequestBuilder
        ModifyResponse response = ldapConnectionTemplate.modify(
                ldapConnectionTemplate.newDn( "uid=misspiggy,ou=muppets,dc=muppets,dc=org" ),
                new RequestBuilder<ModifyRequest>() {
                    @Override
                    public void buildRequest( ModifyRequest request ) throws LdapException {
                        request.replace( "sn", "The Frog" )
                                .replace( "cn", "Miss The Frog" )
                                .replace( "mail", "missthefrog@muppets.org" );
                    }
                } );
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值