首页 技术 正文
技术 2022年11月6日
0 收藏 362 点赞 686 浏览 1772 个字
  • 使用spring-data-ldap的基础用法,定义LDAP中属性与我们Java中定义实体的关系映射以及对应的Repository

    @Data
    @Entry(base = "ou=people,dc=didispace,dc=com", objectClasses = "inetOrgPerson")
    public class Person { @Id
    private Name id;
    @DnAttribute(value = "uid", index = 3)
    private String uid;
    @Attribute(name = "cn")
    private String commonName;
    @Attribute(name = "sn")
    private String suerName;
    private String userPassword;}public interface PersonRepository extends CrudRepository<Person, Name> {}

      

    通过上面的定义之后,已经将Person对象与LDAP存储内容实现了映射,我们只需要使用PersonRepository就可以轻松的对LDAP内容实现读写。

  • 创建单元测试用例读取所有用户信息:
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {@Autowired
    private PersonRepository personRepository;@Test
    public void findAll() throws Exception {
    personRepository.findAll().forEach(p -> {
    System.out.println(p);
    });
    }
    }

      

    启动该测试用例之后,我们可以看到控制台中输出了刚才维护在ldap-server.ldif中的用户信息:

    2018-01-27 14:25:06.283  WARN 73630 --- [           main] o.s.ldap.odm.core.impl.ObjectMetaData    : The Entry class Person should be declared final
    Person(id=uid=ben,ou=people,dc=didispace,dc=com, uid=ben, commonName=didi, suerName=zhaiyongchao, userPassword=123,83,72,65,125,110,70,67,101,98,87,106,120,102,97,76,98,72,72,71,49,81,107,53,85,85,52,116,114,98,118,81,61)

      

    添加用户

    通过上面的入门示例,如果您能够独立完成,那么在Spring Boot中操作LDAP的基础目标已经完成了。

    如果您足够了解Spring Data,其实不难想到,这个在其下的子项目必然也遵守Repsitory的抽象。所以,我们可以使用上面定义的PersonRepository来轻松实现操作,比如下面的代码就可以方便的往LDAP中添加用户:

    Person person = new Person();
    person.setUid("uid:1");
    person.setSuerName("AAA");
    person.setCommonName("aaa");
    person.setUserPassword("123456");
    personRepository.save(person);

      

    如果还想实现更多操作,您可以参考spring-data-ldap的文档来进行使用。

    连接LDAP服务端

    在本文的例子中都采用了嵌入式的LDAP服务器,事实上这种方式也仅限于我们本地测试开发使用,真实环境下LDAP服务端必然是独立部署的。

    在Spring Boot的封装下,我们只需要配置下面这些参数就能将上面的例子连接到远端的LDAP而不是嵌入式的LDAP。

    spring.ldap.urls=ldap://localhost:1235
    spring.ldap.base=dc=didispace,dc=com
    spring.ldap.username=didispace
    spring.ldap.password=123456

      

    Spring Boot教程(四十二)LDAP来管理用户信息(2)

    源码来源
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918