zoukankan      html  css  js  c++  java
  • Spring Boot教程(四十二)LDAP来管理用户信息(2)

    • 使用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
      

        

      源码来源
  • 相关阅读:
    MR架构
    概念词汇
    数仓项目06:DWD层
    Informatic学习总结_day03
    oracle_创建和管理表
    oracle_使用子查询创建表
    oracle数据类型
    文本变语音引擎 ekho
    [LNOI2014]LCA
    POJ 2942 Knights of the Round Table
  • 原文地址:https://www.cnblogs.com/allalongx/p/8532563.html
Copyright © 2011-2022 走看看