LDAP
LDAP(輕量級目錄訪問協(xié)議澳泵,Lightweight Directory Access Protocol)是實現(xiàn)提供被稱為目錄服務(wù)的信息服務(wù)性湿。
目錄服務(wù)是一種特殊的數(shù)據(jù)庫系統(tǒng),其專門針對讀取襟衰,瀏覽和搜索操作進行了特定的優(yōu)化贴铜。目錄一般用來包含描述性的,基于屬性的信息并支持精細復(fù)雜的過濾能力瀑晒。
目錄一般不支持通用數(shù)據(jù)庫針對大量更新操作操作需要的復(fù)雜的事務(wù)管理或回卷策略绍坝。而目錄服務(wù)的更新則一般都非常簡單。
目錄可以存儲包括個人信息苔悦、web鏈結(jié)轩褐、jpeg圖像等各種信息。為了訪問存儲在目錄中的信息玖详,就需要使用運行在TCP/IP 之上的訪問協(xié)議—LDAP把介。
LDAP目錄中的信息是是按照樹型結(jié)構(gòu)組織,具體信息存儲在條目(entry)的數(shù)據(jù)結(jié)構(gòu)中蟋座。條目相當于關(guān)系數(shù)據(jù)庫中表的記錄劳澄;
LDAP簡稱
o - organization
ou - organization unit
c - countryName
dc - domainComponent
sn - suer name
cn - common name
dn - Distinguished Name,LDAP中entry的唯一辨別名
base dn (在下邊的例子中有提到)
執(zhí)行LDAP search時一般要指定base dn蜈七,由于LDAP是樹狀數(shù)據(jù)結(jié)構(gòu)秒拔,指定base dn后,搜索將從base dn開始。
objectClass
結(jié)構(gòu)型(Structural):如account砂缩、inetOrgPerson作谚、person和organizationUnit;
輔助型(Auxiliary):如extensibeObject庵芭;
抽象型(Abstract):如top妹懒,抽象型的objectClass不能直接使用。
規(guī)定了就是說指定為特定的objectClass之后双吆,有些屬性就是必須要有的例如眨唬,account就要求userid是必填項,而inetOrgPerson則要求cn(common name,常用名稱)和sn(sure name,真實名稱)是必填項好乐。
SpringBoot+LDAP
- springboot封裝了嵌入式LDAP內(nèi)存服務(wù)器匾竿,所以方便測試,不需要進行過多的安裝蔚万。
項目結(jié)構(gòu)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>
<!-- LDAP測試-->
<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<scope>test</scope>
</dependency>
Person.java
package com.inverseli.learning.entity;
import javax.naming.Name;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
import lombok.Data;
/**
* @date:2018年9月24日 上午12:24:44
* @author liyuhao
* @version 1.0
* @since JDK 1.8
* @description:
*/
@Entry(base = "ou=people,dc=learning,dc=inverseli,dc=com",objectClasses="inetOrgPerson")
@Data
public class Person {
@Id
private Name id;
@DnAttribute(value="uid",index = 3)
private String uid;
@Attribute(name = "cn")
private String commonName;
@Attribute(name = "sn")
private String suerName;
private String userPassword;
}
PersonRepository.java
package com.inverseli.learning;
import javax.naming.Name;
import org.springframework.data.repository.CrudRepository;
import com.inverseli.learning.entity.Person;
/**
* @date:2018年9月24日 上午12:30:56
* @author liyuhao
* @version 1.0
* @since JDK 1.8
* @description:
*/
public interface PersonRepository extends CrudRepository<Person, Name>{
// extends CrudRepository to crud
}
src/test/resources/application.properties
# classpath
spring.ldap.embedded.ldif=classpath:ldap-server.ldif
spring.ldap.embedded.base-dn=dc=learning,dc=inverseli,dc=com
ldap-server.ldif
# 注釋的內(nèi)容都是我自己的理解岭妖,可能會有不對的地方
# com.inverseli.learning是基本dn,從這里開始搜索
dn: dc=learning,dc=inverseli,dc=com
objectClass: top
objectClass: domain
# base dn的分支 people
dn: ou=people,dc=learning,dc=inverseli,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people
# 這里就是一個person對象了
dn: uid=inverse,ou=people,dc=learning,dc=inverseli,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
# 這里是用戶信息
cn: inverse
sn: lyh
uid: inverse
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=
踩過的坑
SpringBoot的版本用的是1.5.9版本反璃,剛開始用的是2.0.0版本昵慌,不知道新版本哪里更新了,測試的時候總是出現(xiàn)空指針問題淮蜈,還沒有解決斋攀。