本文是Spring Boot系列文集中關于LDAP連接相關操作的一文。僅僅涉及基本的使用ODM來快速實現LDAP增刪改查操作。詳細的關于Spring LDAP的其他操作,可以參考翻譯的官方文檔。
本文目的:使用Spring Boot構建項目,幫助讀者快速配置并使用Spring LDAP操作LDAP。大致步驟如下:
1.創(chuàng)建Spring Boot項目(約1分鐘)
2.添加pom.xml文件中Spring LDAP依賴(約1分鐘)
3.配置Spring LDAP連接信息(約1分鐘)
4.創(chuàng)建實體類作為LDAP中的entry映射(ODM映射功能,類似ORM)
5.使用ldapTemplate書寫service層的方法(約3分鐘)
6.編寫controller層(約3分鐘)
1.創(chuàng)建Spring Boot項目(約1分鐘)
IDEA中點擊file - new - project
圖1
如上圖,選擇左側的 Spring Initializr幫助初始化spring項目,配置好SDK后,點擊next。
圖2
點擊后,如圖2,如果只是做demo,該頁面默認即可,點擊next。
圖3
如圖3,我們選擇web,右側會顯示web相關的組件,我們選擇右側中的Web,將其前面的框勾選上。這代表在創(chuàng)建的spring boot項目中會引入web相關的依賴。點擊next。
圖4
如圖4,這里自己命名即可,點擊finish。
2.添加pom.xml文件中Spring LDAP依賴(約1分鐘)
圖5
如上圖圖5,在項目中雙擊pom.xml來添加依賴。
圖6
如圖6所示,文件中已經加載了spring-boot-starter-web依賴,我們要使用Spring LDAP來操作LDAP服務器需要添加spring-boot-starter-data-ldap。該依賴會自動加載spring-ldap-core 與 spring-data-ldap依賴。其中spring-ldap-core是ldap操作的核心依賴,而spring-data-ldap提供了ODM的功能,能夠簡化操作。我們可以在項目的External Libraries中看到這兩個依賴,如下圖圖7中三個黃色高亮處:
圖7
3.配置Spring LDAP連接信息
圖8
如上圖圖8,根據spring boot官網對ldap配置的說明來配置,可以看這里。這樣配置之后,spring boot會自動讀取該配置。
4.創(chuàng)建實體類作為LDAP中的entry映射
本例中使用ODM功能,極大的簡化了LDAP的操作,關于ODM更多的信息,可以參考翻譯的官方文檔。
我們在項目中創(chuàng)建如下結構:
圖9
現在,我們在entry包下寫與entry互相映射的實體類。其中,我的LDAP結構如下
圖10
新建Person類
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 + '/'' + '}'; }}
注意@Entry與@Id為必須的。而@JsonIgnore是為了將person傳給前端時不報錯,因為Name類型的無法自動解析成json格式。注意我為了方便,在 public Person(String cn) {}構造方法中寫上了DN值的生成方法,在setCn中也寫上了該方法,當然存在代碼重復問題,忽略就好。
5.使用ldapTemplate書寫service層的方法
在service包中,新建OdmPersonRepo類
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); }}
可以看到,基本的增刪改查操作都幫我們實現了,我們只要調用一下ldapTemplate中的方法即可。若要更自由的操作ldap的增刪改查,可參閱翻譯的官方文檔。
6.編寫controller層
在controller包下,新建一個testController類來測試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); }}
至此,一個基本的demo完成啦。下面我們測試一下
測試
為了大家都能跟著步驟來,我就不使用Postman來測試,而是在瀏覽器中測試接口。、
啟動spring boot,沒有報錯的話,打開瀏覽器到 localhost:8080/
,按下F12,彈出開發(fā)者模式,找到console控制臺方便我們發(fā)送測試語句。
首先,引入jquery.js。打開jquery.js,全選-復制-在console中粘貼-回車,如下圖:
圖11
顯示為true,代表加載成功,我們可以使用jquery的ajax來測試了。
新增數據
圖12
正如controller層的testController要求的那樣,我們在地址 /create 上使用post方法,將數據cn sn userPassword傳過去
圖13
而在LDAP服務器中,也顯示了新增的數據
圖14
查找數據
圖15
也能根據cn正確查找到數據。
修改數據
圖16
我們查看LDAP中是否修改
圖17
可以看到能夠正常修改數據
刪除數據
圖18
查看LDAP中是否刪除
圖19
可以看到,數據被正確刪除了。
其他說明
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
|
新聞熱點
疑難解答
圖片精選