当前位置:   article > 正文

Spring Boot集成Ldap快速入门Demo

Spring Boot集成Ldap快速入门Demo

1.Ldap介绍

LDAP,Lightweight Directory Access Protocol,轻量级目录访问协议.

  1. LDAP是一种特殊的服务器,可以存储数据
  2. 数据的存储是目录形式的,或者可以理解为树状结构(一层套一层)
  3. 一般存储关于用户、用户认证信息、组、用户成员,通常用于用户认证与授权

LDAP简称对应

  • o:organization(组织-公司)
  • ou:organization unit(组织单元-部门)
  • c:countryName(国家)
  • dc:domainComponent(域名)
  • sn:surname(姓氏)
  • cn:common name(常用名称)

2.环境搭建

docker-compose-ldap.yaml

  1. version: '3'
  2. services:
  3. openldap:
  4. container_name: openldap
  5. image: osixia/openldap:latest
  6. ports:
  7. - "8389:389"
  8. - "8636:636"
  9. volumes:
  10. - ~/ldap/backup:/data/backup
  11. - ~/ldap/data:/var/lib/openldap
  12. - ~/ldap/config:/etc/openldap/slapd.d
  13. - ~/ldap/certs:/assets/slapd/certs
  14. command: [--copy-service, --loglevel, debug]
  15. phpldapadmin:
  16. container_name: phpldapadmin
  17. image: osixia/phpldapadmin:latest
  18. ports:
  19. - "8080:80"
  20. environment:
  21. - PHPLDAPADMIN_HTTPS="false"
  22. - PHPLDAPADMIN_LDAP_HOSTS=openldap
  23. links:
  24. - openldap
  25. depends_on:
  26. - openldap

ldap setup

docker-compose -f docker-compose-ldap.yml -p ldap up -d

open http://localhost:8080/

default account

  1. username:cn=admin,dc=example,dc=org
  2. password:admin

init data

  1. dn: ou=people,dc=exapmple,dc=org
  2. objectClass: top
  3. objectClass: organizationalUnit
  4. ou: people

58

3.代码工程

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>ldap</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <!--ldap-->
  31. <dependency>
  32. <groupId>org.springframework.boot</groupId>
  33. <artifactId>spring-boot-starter-data-ldap</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.projectlombok</groupId>
  37. <artifactId>lombok</artifactId>
  38. </dependency>
  39. </dependencies>
  40. </project>

application.yaml

  1. spring:
  2. application:
  3. name: spring-demo-ldap
  4. # ldap configuration
  5. ldap:
  6. urls: ldap://127.0.0.1:8389
  7. base: dc=example,dc=org
  8. username: cn=admin,${spring.ldap.base}
  9. password: admin
  10. server:
  11. port: 8088

Person.java

  1. package com.et.ldap.entity;
  2. import lombok.Data;
  3. import org.springframework.ldap.odm.annotations.Attribute;
  4. import org.springframework.ldap.odm.annotations.DnAttribute;
  5. import org.springframework.ldap.odm.annotations.Entry;
  6. import org.springframework.ldap.odm.annotations.Id;
  7. import javax.naming.Name;
  8. import java.io.Serializable;
  9. @Data
  10. @Entry(base = "ou=people", objectClasses="inetOrgPerson")
  11. public class Person implements Serializable {
  12. private static final long serialVersionUID = -337113594734127702L;
  13. /**
  14. *neccesary
  15. */
  16. @Id
  17. private Name id;
  18. @DnAttribute(value = "uid", index = 3)
  19. private String uid;
  20. @Attribute(name = "cn")
  21. private String commonName;
  22. @Attribute(name = "sn")
  23. private String suerName;
  24. private String userPassword;
  25. }

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

4.测试

  1. package com.et.ldap;
  2. import com.et.ldap.entity.Person;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. import org.springframework.ldap.NamingException;
  8. import org.springframework.ldap.core.AttributesMapper;
  9. import org.springframework.ldap.core.LdapTemplate;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11. import javax.naming.NamingEnumeration;
  12. import javax.naming.directory.Attribute;
  13. import javax.naming.directory.Attributes;
  14. import java.util.List;
  15. import static org.springframework.ldap.query.LdapQueryBuilder.query;
  16. @RunWith(SpringRunner.class)
  17. @SpringBootTest
  18. public class ApplicationTests {
  19. @Autowired
  20. private LdapTemplate ldapTemplate;
  21. /**
  22. * add person
  23. */
  24. @Test
  25. public void addPerson() {
  26. Person person = new Person();
  27. person.setUid("uid:14");
  28. person.setSuerName("LISI");
  29. person.setCommonName("lisi");
  30. person.setUserPassword("123456");
  31. ldapTemplate.create(person);
  32. }
  33. /**
  34. * filter search
  35. */
  36. @Test
  37. public void filterSearch() {
  38. // Get the domain list. If you want to get a certain domain, the filter can be written like this: (&(objectclass=dcObject)&(dc=example))
  39. // String filter = "(&(objectclass=dcObject))";
  40. // Get the list of organizations. If you want to get a specific organization, the filter can be written like this: (&(objectclass=organizationalUnit)&(ou=people)
  41. // String filter = "(&(objectclass=organizationalUnit))";
  42. //Get the people list. If you want to get a certain person, the filter can be written like this: (&(objectclass=inetOrgPerson)&(uid=uid:13))
  43. String filter = "(&(objectclass=inetOrgPerson))";
  44. List<Person> list = ldapTemplate.search("", filter, new AttributesMapper() {
  45. @Override
  46. public Object mapFromAttributes(Attributes attributes) throws NamingException, javax.naming.NamingException {
  47. //如果不知道ldap中有哪些属性,可以使用下面这种方式打印
  48. NamingEnumeration<? extends Attribute> att = attributes.getAll();
  49. while (att.hasMore()) {
  50. Attribute a = att.next();
  51. System.out.println(a.getID() + "=" + a.get());
  52. }
  53. Person p = new Person();
  54. Attribute a = attributes.get("cn");
  55. if (a != null) p.setCommonName((String) a.get());
  56. a = attributes.get("uid");
  57. if (a != null) p.setUid((String) a.get());
  58. a = attributes.get("sn");
  59. if (a != null) p.setSuerName((String) a.get());
  60. a = attributes.get("userPassword");
  61. if (a != null) p.setUserPassword(a.get().toString());
  62. return p;
  63. }
  64. });
  65. list.stream().forEach(System.out::println);
  66. }
  67. /**
  68. * query search
  69. */
  70. @Test
  71. public void querySearch() {
  72. // You can also use filter query method, filter is (&(objectClass=user)(!(objectClass=computer))
  73. List<Person> personList = ldapTemplate.search(query()
  74. .where("objectClass").is("inetOrgPerson")
  75. .and("uid").is("uid:14"),
  76. new AttributesMapper() {
  77. @Override
  78. public Person mapFromAttributes(Attributes attributes) throws NamingException, javax.naming.NamingException {
  79. //If you don’t know what attributes are in ldap, you can print them in the following way
  80. // NamingEnumeration<? extends Attribute> att = attr.getAll();
  81. //while (att.hasMore()) {
  82. // Attribute a = att.next();
  83. // System.out.println(a.getID());
  84. //}
  85. Person p = new Person();
  86. Attribute a = attributes.get("cn");
  87. if (a != null) p.setCommonName((String) a.get());
  88. a = attributes.get("uid");
  89. if (a != null) p.setUid((String) a.get());
  90. a = attributes.get("sn");
  91. if (a != null) p.setSuerName((String) a.get());
  92. a = attributes.get("userPassword");
  93. if (a != null) p.setUserPassword(a.get().toString());
  94. return p;
  95. }
  96. });
  97. personList.stream().forEach(System.out::println);
  98. }
  99. }

运行单元测试类,查看数据,可以看到新增一个人

80

5.引用参考

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/580009
推荐阅读
相关标签
  

闽ICP备14008679号