本文是spring boot系列文集中關于ldap連接相關操作的一文。僅僅涉及基本的使用odm來快速實現ldap增刪改查操作。詳細的關于spring ldap的其他操作,可以參考翻譯的官方文檔。
本文目的:使用spring boot構建項目,幫助讀者快速配置并使用spring ldap操作ldap。大致步驟如下:
1.創建spring boot項目(約1分鐘)
2.添加pom.xml文件中spring ldap依賴(約1分鐘)
3.配置spring ldap連接信息(約1分鐘)
4.創建實體類作為ldap中的entry映射(odm映射功能,類似orm)
5.使用ldaptemplate書寫service層的方法(約3分鐘)
6.編寫controller層(約3分鐘)
1.創建spring boot項目(約1分鐘)
idea中點擊file - new - project
圖1
如上圖,選擇左側的 spring initializr幫助初始化spring項目,配置好sdk后,點擊next。
圖2
點擊后,如圖2,如果只是做demo,該頁面默認即可,點擊next。
圖3
如圖3,我們選擇web,右側會顯示web相關的組件,我們選擇右側中的web,將其前面的框勾選上。這代表在創建的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.創建實體類作為ldap中的entry映射
本例中使用odm功能,極大的簡化了ldap的操作,關于odm更多的信息,可以參考翻譯的官方文檔。
我們在項目中創建如下結構:
圖9
現在,我們在entry包下寫與entry互相映射的實體類。其中,我的ldap結構如下
圖10
新建person類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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: */ @service public 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的操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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: */ @restcontroller public 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,彈出開發者模式,找到console控制臺方便我們發送測試語句。
首先,引入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
可以看到,數據被正確刪除了。
其他說明
- 剛才的例子中,代碼有需要完善的地方,但對于demo演示來說完全可以忍受。大家可能也看到了這么做也有些缺點,我在update的時候,需要將修改后的person的所有屬性值都傳到后臺來(這也不算啥缺點,關系數據庫的更新也是這樣),并且不能修改cn的值(這就是為什么其他例子中都是使用uid來作為dn的一部分,類似于關系數據庫的主鍵的作用),因為修改后該entry的dn值就變化了,odm就無法確定更新哪個數據。會報 javax.naming.namenotfoundexception: [ldap: error code 32 - no such object] 錯誤。
- 刪除操作也像關系數據庫的操作一樣,直接給cn即可,這是因為我們在person類中setcn()方法內寫了dn的生成函數,這樣odm才能根據被@id所注釋的dn來找到ldap中的entry并執行刪除操作。
- 我們在person類中寫了name類型的dn值的構建方法,但是我一開始按照官網的代碼來寫,總是出問題,在stackoverflow中找到了答案。鏈接在這里。
- 想要更深入的了解,可以參考翻譯的官方文檔。了解更自由更個性化的操作。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/316fbb6bfd81