1. 程式人生 > >企業分散式微服務雲SpringCloud SpringBoot mybatis (十六)Spring Boot中使用LDAP來統一管理使用者資訊

企業分散式微服務雲SpringCloud SpringBoot mybatis (十六)Spring Boot中使用LDAP來統一管理使用者資訊

LDAP簡介

LDAP(輕量級目錄訪問協議,Lightweight Directory Access Protocol)是實現提供被稱為目錄服務的資訊服務。目錄服務是一種特殊的資料庫系統,其專門針對讀取,瀏覽和搜尋操作進行了特定的優化。目錄一般用來包含描述性的,基於屬性的資訊並支援精細複雜的過濾能力。目錄一般不支援通用資料庫針對大量更新操作操作需要的複雜的事務管理或回捲策略。而目錄服務的更新則一般都非常簡單。這種目錄可以儲存包括個人資訊、web鏈結、jpeg影象等各種資訊。為了訪問儲存在目錄中的資訊,就需要使用執行在TCP/IP 之上的訪問協議—LDAP。

LDAP目錄中的資訊是是按照樹型結構組織,具體資訊儲存在條目(entry)的資料結構中。條目相當於關係資料庫中表的記錄;條目是具有區別名DN (Distinguished Name)的屬性(Attribute),DN是用來引用條目的,DN相當於關係資料庫表中的關鍵字(Primary Key)。屬性由型別(Type)和一個或多個值(Values)組成,相當於關係資料庫中的欄位(Field)由欄位名和資料型別組成,只是為了方便檢索的需要,LDAP中的Type可以有多個Value,而不是關係資料庫中為降低資料的冗餘性要求實現的各個域必須是不相關的。LDAP中條目的組織一般按照地理位置和組織關係進行組織,非常的直觀。LDAP把資料存放在檔案中,為提高效率可以使用基於索引的檔案資料庫,而不是關係資料庫。型別的一個例子就是mail,其值將是一個電子郵件地址。

LDAP的資訊是以樹型結構儲存的,在樹根一般定義國家(c=CN)或域名(dc=com),在其下則往往定義一個或多個組織 (organization)(o=Acme)或組織單元(organizational units) (ou=People)。一個組織單元可能包含諸如所有僱員、大樓內的所有印表機等資訊。此外,LDAP支援對條目能夠和必須支援哪些屬性進行控制,這是有一個特殊的稱為物件類別(objectClass)的屬性來實現的。該屬性的值決定了該條目必須遵循的一些規則,其規定了該條目能夠及至少應該包含哪些屬性。例如:inetorgPerson物件類需要支援sn(surname)和cn(common name)屬性,但也可以包含可選的如郵件,電話號碼等屬性。

LDAP簡稱對應

  • o:organization(組織-公司)
  • ou:organization unit(組織單元-部門)
  • c:countryName(國家)
  • dc:domainComponent(域名)
  • sn:surname(姓氏)
  • cn:common name(常用名稱)

入門示例

在瞭解了LDAP的基礎概念之後,我們通過一個簡單例子進一步理解!

  • 建立一個基礎的Spring Boot專案(如果您還不會,可以參考這兩篇文章:入門1入門2

  • pom.xml中引入兩個重要依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-ldap</artifactId>
</dependency>

<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
    <scope>test</scope>
</dependency>

  • 其中,spring-boot-starter-data-ldap是Spring Boot封裝的對LDAP自動化配置的實現,它是基於spring-data-ldap來對LDAP服務端進行具體操作的。

    unboundid-ldapsdk主要是為了在這裡使用嵌入式的LDAP服務端來進行測試操作,所以scope設定為了test,實際應用中,我們通常會連線真實的、獨立部署的LDAP伺服器,所以不需要此項依賴。

  • src/test/resources目錄下建立ldap-server.ldif檔案,用來儲存LDAP服務端的基礎資料,以備後面的程式訪問之用。
dn: dc=didispace,dc=com
objectClass: top
objectClass: domain

dn: ou=people,dc=didispace,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=ben,ou=people,dc=didispace,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: didi
sn: zhaiyongchao
uid: didi
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=

  • 這裡建立了一個基礎使用者,真實姓名為zhaiyongchao,常用名didi,在後面的程式中,我們會來讀取這些資訊。更多內容解釋大家可以深入學習LDAP來理解,這裡不做過多的講解。

  • application.properties中新增嵌入式LDAP的配置
spring.ldap.embedded.ldif=ldap-server.ldif
spring.ldap.embedded.base-dn=dc=didispace,dc=com

使用spring-data-ldap的基礎用法,定義LDAP中屬性與我們Java中定義實體的關係對映以及對應的Repository
@Data
@Entry(base = "ou=people,dc=didispace,dc=com", objectClasses = "inetOrgPerson")
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;

}

public interface PersonRepository extends CrudRepository<Person, Name> {

}

  • 通過上面的定義之後,已經將Person物件與LDAP儲存內容實現了對映,我們只需要使用PersonRepository就可以輕鬆的對LDAP內容實現讀寫。

  • 建立單元測試用例讀取所有使用者資訊:
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

	@Autowired
	private PersonRepository personRepository;

	@Test
	public void findAll() throws Exception {
		personRepository.findAll().forEach(p -> {
			System.out.println(p);
		});
	}
}

啟動該測試用例之後,我們可以看到控制檯中輸出了剛才維護在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 be declared final
Person(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)

新增使用者

通過上面的入門示例,如果您能夠獨立完成,那麼在Spring Boot中操作LDAP的基礎目標已經完成了。

如果您足夠了解Spring Data,其實不難想到,這個在其下的子專案必然也遵守Repsitory的抽象。所以,我們可以使用上面定義的PersonRepository來輕鬆實現操作,比如下面的程式碼就可以方便的往LDAP中新增使用者:

Person person = new Person();
person.setUid("uid:1");
person.setSuerName("AAA");
person.setCommonName("aaa");
person.setUserPassword("123456");
personRepository.save(person);

如果還想實現更多操作,您可以參考spring-data-ldap的文件來進行使用。

連線LDAP服務端

在本文的例子中都採用了嵌入式的LDAP伺服器,事實上這種方式也僅限於我們本地測試開發使用,真實環境下LDAP服務端必然是獨立部署的。

在Spring Boot的封裝下,我們只需要配置下面這些引數就能將上面的例子連線到遠端的LDAP而不是嵌入式的LDAP。

spring.ldap.urls=ldap://localhost:1235
spring.ldap.base=dc=didispace,dc=com
spring.ldap.username=didispace
spring.ldap.password=123456

原始碼來源