This article is an article about LDAP connection-related operations in the Spring Boot series. It only involves the basic use of ODM to quickly implement LDAP addition, deletion, modification and search operations. For detailed information about Spring LDAP, please refer to the translated official documentation.
The purpose of this article: Use Spring Boot to build projects to help readers quickly configure and operate LDAP using Spring LDAP. The general steps are as follows:
1. Create Spring Boot Project (about 1 minute)
2. Add Spring LDAP dependency in pom.xml file (about 1 minute)
3. Configure Spring LDAP connection information (about 1 minute)
4. Create entity classes as entry map in LDAP (ODM mapping function, similar to ORM)
5. Method of writing service layer using ldapTemplate (about 3 minutes)
6. Write the controller layer (about 3 minutes)
1. Create Spring Boot Project (about 1 minute)
Click file - new - project in IDEA
Figure 1
As shown in the figure above, select Spring Initializr on the left to help initialize the spring project. After configuring the SDK, click next.
Figure 2
After clicking, as shown in Figure 2, if you just do demo, the page can be done by default, click next.
Figure 3
As shown in Figure 3, we select the web, and the components related to the web will be displayed on the right side. We select the Web on the right and check the box in front of it. This means that web-related dependencies will be introduced in the created spring boot project. Click next.
Figure 4
As shown in Figure 4, just name it yourself here, click finish.
2. Add Spring LDAP dependency in pom.xml file (about 1 minute)
Figure 5
As shown in Figure 5 above, double-click pom.xml in the project to add dependencies.
Figure 6
As shown in Figure 6, the file has been loaded with spring-boot-starter-web dependencies. If we want to use Spring LDAP to operate the LDAP server, we need to add spring-boot-starter-data-ldap. This dependency will automatically load spring-ldap-core and spring-data-ldap dependencies. Among them, spring-ldap-core is the core dependency of ldap operations, while spring-data-ldap provides ODM functions to simplify operations. We can see these two dependencies in the External Libraries of the project, as shown in the following figure 7:
Figure 7
3. Configure Spring LDAP connection information
Figure 8
As shown in Figure 8 above, the configuration is based on the instructions on the ldap configuration on the official spring boot website, you can see here. After this configuration, spring boot will automatically read the configuration.
4. Create entity class as entry map in LDAP
In this example, the ODM function is used to greatly simplify the operation of LDAP. For more information about ODM, you can refer to the translated official documents.
We create the following structure in the project:
Figure 9
Now, we write the entity class that maps each other with the entry package. Among them, my LDAP structure is as follows
Figure 10
Create a new Person class
package com.example.demo.entry;import com.fasterxml.jackson.annotation.JsonIgnore;import org.springframework.ldap.odm.annotations.Attribute;import org.springframework.ldap.odm.annotations.Entry;import org.springframework.ldap.odm.annotations.Id;import org.springframework.ldap.support.LdapNameBuilder;import javax.naming.Name;/** * @Author: geng_pool * @Description: * @Date: Created in 2017/12/27 10:24 * @Modified by: */@Entry(objectClasses = {"organizationalPerson","person","top"},base = "o=myorg")public class Person { @Id @JsonIgnore private Name dn; @Attribute(name="cn") private String cn; @Attribute(name="sn") private String sn; @Attribute(name="userPassword") private String userPassword; public Person(String cn) { Name dn = LdapNameBuilder.newInstance() .add("o", "myorg") .add("cn", cn) .build(); this.dn = dn; } public Person(){} /* getter */ public Name getDn() { return dn; } public String getCn() { return cn; } public String getSn() { return sn; } public String getUserPassword() { return userPassword; } /* setter */ public void setDn(Name dn) { this.dn = dn; } public void setCn(String cn) { this.cn = cn; if(this.dn==null){ Name dn = LdapNameBuilder.newInstance() .add("o", "myorg") .add("cn", cn) .build(); this.dn = dn; } } public void setSn(String sn) { this.sn = sn; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } @Override public String toString() { return "Person{" + "dn=" + dn.toString() + ", cn='" + cn + '/'' + ", sn='" + sn + '/'' + ", userPassword='" + userPassword + '/'' + '}'; }}Note that @Entry and @Id are required. @JsonIgnore is to not report an error when passing person to the front end, because Name type cannot be automatically parsed into json format. Note that for convenience, I wrote the DN value generation method in the public Person(String cn) {} constructor, and also wrote the method in setCn. Of course, there is a problem of code duplication, just ignore it.
5. Method of writing service layer using ldapTemplate
In the service package, create a new OdmPersonRepo class
package com.example.demo.service;import com.example.demo.entry.Person;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.ldap.core.LdapTemplate;import org.springframework.stereotype.Service;import static org.springframework.ldap.query.LdapQueryBuilder.query;/** * @Author: geng_pool * @Description: * @Date: Created in 2017/12/27 10:37 * @Modified by: */@Servicepublic class OdmPersonRepo { @Autowired private LdapTemplate ldapTemplate; public Person create(Person person){ ldapTemplate.create(person); return person; } public Person findByCn(String cn){ return ldapTemplate.findOne(query().where("cn").is(cn),Person.class); } public Person modifyPerson(Person person){ ldapTemplate.update(person); return person; } public void deletePerson(Person person){ ldapTemplate.delete(person); }}As you can see, the basic addition, deletion, modification and search operations have been implemented for us. We just need to call the method in ldapTemplate. To operate ldap's additions, deletions, modifications and searches more freely, you can refer to the translated official documents.
6. Write the controller layer
Under the controller package, create a new testController class to test the operation of LDAP.
package com.example.demo.controller;import com.example.demo.entry.Person;import com.example.demo.service.OdmPersonRepo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.ldap.core.LdapTemplate;import org.springframework.web.bind.annotation.*;/** * @Author: geng_pool * @Description: * @Date: Created in 2017/12/27 10:50 * @Modified by: */@RestControllerpublic class testController { @Autowired private OdmPersonRepo odmPersonRepo; @RequestMapping(value = "/findOne",method = RequestMethod.POST) public Person findByCn(@RequestParam(name = "cn",required = true) String cn){ return odmPersonRepo.findByCn(cn); } @PostMapping(value = "/create") public Person create(@RequestParam(name = "cn") String cn,@RequestParam(name = "sn") String sn,@RequestParam(name = "userPassword") String userPassworld){ Person person = new Person(); person.setCn(cn); person.setSn(sn); person.setUserPassword(userPassworld); return odmPersonRepo.create(person); } @PostMapping(value = "/update") public Person update(@RequestParam(name = "cn") String cn,@RequestParam(name = "sn") String sn,@RequestParam(name = "userPassword") String userPassworld){ Person person = new Person(); person.setCn(cn); person.setSn(sn); person.setUserPassword(userPassworld); return odmPersonRepo.modifyPerson(person); } @PostMapping(value = "/delete") public void delete(@RequestParam(name = "cn")String cn){ Person person = new Person(); person.setCn(cn); odmPersonRepo.deletePerson(person); }}At this point, a basic demo has been completed. Let's test it below
test
In order for everyone to follow the steps, I will not use Postman to test, but test the interface in the browser. ,
Start spring boot. If there is no error, open the browser to localhost:8080/ , press F12, and pop up the developer mode. Find the console console to facilitate us to send test statements.
First, introduce jquery.js. Open jquery.js, select all - copy - paste in console - enter, as shown in the figure below:
Figure 11
Shown as true, which means the loading is successful. We can use jquery's ajax to test it.
Added data
Figure 12
As required by the testController of the controller layer, we use the post method on the address/create to pass the data cn sn userPassword over
Figure 13
In the LDAP server, the added data is also displayed
Figure 14
Find data
Figure 15
The data can also be correctly found based on CN.
Modify data
Figure 16
Let's check if the LDAP is modified
Figure 17
You can see that the data can be modified normally
Delete data
Figure 18
Check whether to delete in LDAP
Figure 19
As you can see, the data has been deleted correctly.
Other Instructions
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.