Create a unit test case to read all user information:@RunWith (springrunner.class) @SpringBootTestpublic class Applicationtests {@Autowiredprivate personrepository personrepository; @Testpublic void FindAll () throws Exception {Personrepository.findall (). ForEach (P, { SYSTEM.OUT.PRINTLN (P);});}}
After starting the test case, we can see the console output the user information that was just maintained in 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 is declared Finalperson (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)
Add userWith the Getting started example above, if you can do it independently, the base target for operating LDAP in spring boot is complete.
If you are familiar with spring Data, it is not difficult to think that this sub-project under it must also adhere to the repsitory abstraction. Therefore, we can use the above definition PersonRepository
to easily implement the operation, such as the following code can be easily added to LDAP users:
person person = new person ();p erson.setuid ("uid:1");p erson.setsuername ("AAA");p erson.setcommonname ("AAA"); Person.setuserpassword ("123456");p ersonrepository.save (person);
If you want to do more, you can refer to the SPRING-DATA-LDAP documentation for use.
Connecting to the LDAP Service sideIn this case, the embedded LDAP server is used, in fact, this method is limited to our local testing and development, and the LDAP Service side must be deployed independently in the real environment.
In the Spring boot package, we only need to configure the following parameters to connect the above example to the remote LDAP instead of the embedded LDAP.
Spring.ldap.urls=ldap://localhost:1235spring.ldap.base=dc=didispace,dc=comspring.ldap.username= didispacespring.ldap.password=123456
Source Source