LDAP-初见

什么是LDAP?

LDAP 的全称是 Lightweight Directory Access Protocol,「轻量目录访问协议」

所以说,LDAP 「是一个协议」,约定了 Client 与 Server 之间的信息交互格式、使用的端口号、认证方式等内容。而 「LDAP 协议的实现」,有着众多版本,例如微软的 Active Directory 是 LDAP 在 Windows 上的实现,AD 实现了 LDAP 所需的树形数据库、具体如何解析请求数据并到数据库查询然后返回结果等功能。再例如 OpenLDAP 是可以运行在 Linux 上的 LDAP 协议的开源实现。而我们平常说的 LDAP Server,一般指的是安装并配置了 Active Directory、OpenLDAP 这些程序的服务器。

历史原因,LDAP 协议诞生于 1988 年,比万维网的诞生还要早。1989 年英国科学家蒂姆·伯纳斯-李发明了万维网。因此沿用至今。树形用户目录,树形存储结构,对组织管理建模符合直觉。开放的标准化协议,受到广泛支持。

目录服务就是按照「树状」存储信息的模式。目录服务的数据类型主要是「字符型」, 而不是关系数据库提供的整数、浮点数、日期、货币等类型。为了检索的需要添加了 BIN(二进制数据)、CIS(忽略大小写)、CES(大小写敏感)、TEL(电话型)等语法(Syntax)。同样也不提供象关系数据库中普遍包含的大量的函数。目录有很强的查询(读)功能,适合于进行大量数据的检索;但目录一般只执行简单的更新(写)操作,不支持批量更新所需要的事务处理功能;它主要面向数据的查询服务(查询和修改操作比一般是大于 10:1),不提供事务的回滚(rollback)机制;目录具有广泛复制信息的能力,适合于多个目录服务器同步/更新。

LDAP 协议能解决什么问题?

要说 LDAP 协议能解决什么问题,那不得不提 AD。AD 是 Windows 服务器上最强大的功能,AD 是基于 LDAP 协议的一套解决方案(LDAP 服务器 + 应用),解决了细粒度的权限控制。核心:「谁 以什么权限 访问什么」

Spring Boot中使用LDAP来统一管理用户信息

  • 创建一个基础的Spring Boot项目

  • pom.xml中引入两个重要依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-ldap</artifactId>
    </dependency>
    
    <dependency>
        <groupId>com.unboundid</groupId>
        <artifactId>unboundid-ldapsdk</artifactId>
        <scope>test</scope>
    </dependency>
    
    其中,spring-boot-starter-data-ldap是Spring Boot封装的对LDAP自动化配置的实现,
    它是基于spring-data-ldap来对LDAP服务端进行具体操作的。
    
    而unboundid-ldapsdk主要是为了在这里使用嵌入式的LDAP服务端来进行测试操作,所以scope设置为了test,
    实际应用中,我们通常会连接真实的、独立部署的LDAP服务器,所以不需要此项依赖。
    
  • src/test/resources目录下创建ldap-server.ldif文件,用来存储LDAP服务端的基础数据,以备后面的程序访问之用。

dn: dc=didispace,dc=com
objectClass: top
objectClass: domain

dn: ou=people,dc=didispace,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=ben,ou=people,dc=didispace,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: didi
sn: lijiatu
uid: ljt
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=
  • application.properties中添加嵌入式LDAP的配置

    spring.ldap.embedded.ldif=ldap-server.ldif
    spring.ldap.embedded.base-dn=dc=didispace,dc=com
    
  • 使用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中的用户信息

添加用户

我们可以使用上面定义的PersonRepository来轻松实现操作,比如下面的代码就可以方便的往LDAP中添加用户:

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

连接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
posted @ 2021-10-20 21:17  Ricardo_ML  阅读(264)  评论(0编辑  收藏  举报