Recent projects need to integrate LDAP login, so a simple study a little ldap integration mainly in the following steps:
First, configure the spring boot LDAP configuration file
1. pom configuration file:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-ldap</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies>
2. The configuration data of the meta information LDAP:
spring.ldap.urls=ldap://127.0.0.1:389 spring.ldap.base=DC=dc-test,DC=com spring.ldap.username=cn=cnName,ou=ouName,dc=dc-test,dc=com spring.ldap.password=******
Such information can be normal configuration data query ldap of the
Second, the query examples:
ldapTemplate.lookup("OU=ouName", new AttributesMapper<Object>() { @Override public Object mapFromAttributes(Attributes attributes) throws NamingException { return attributes; } });
A start has been unable to query data suggest:
org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - 0000208D: NameErr: DSID-03100238, problem 2001 (NO_OBJECT), data 0, best match
The reason is that the conditions of the query is wrong, I should start here query query from my side given ou such as my side is given ouName so it should start from ouName query, if you need to query the lower two conditions For example, enter a query with
ldapTemplate.lookup("OU=ouNameNext,OU=ouName", new AttributesMapper<Object>() { @Override public Object mapFromAttributes(Attributes attributes) throws NamingException { return attributes; } });
Note that the structure should be left is subordinate departments, the right is the higher authorities
Third, the last recursive query all departments and members of the information:
@Test public void getAllTree() { List<NameClassPair> nameList = new ArrayList<>(); getCurrentDept("OU=ouName", nameList); } private void getCurrentDept(String base, List<NameClassPair> nameList) { List<NameClassPair> nameClassPairMapperList = ldapTemplate.list(base, new NameClassPairMapper() { @Override public NameClassPair mapFromNameClassPair(NameClassPair nameClassPair) throws NamingException { return nameClassPair; } }); if (nameClassPairMapperList.size() == 0) { return; } for (NameClassPair nameClassPair : nameClassPairMapperList) { // System.out.println(nameClassPair.getNameInNamespace().substring(0, nameClassPair.getNameInNamespace().indexOf("DC") - 1)); getCurrentDept(nameClassPair.getNameInNamespace().substring(0, nameClassPair.getNameInNamespace().indexOf("DC") - 1), nameList); } nameList.addAll(nameClassPairMapperList); }