簡介
本節提供 Spring LDAP 的快速簡介。內容包含以下項目
總覽
Spring LDAP 旨在簡化 Java 中的 LDAP 程式設計。此函式庫提供的一些功能包括:
-
JdbcTemplate
風格的樣板簡化 LDAP 程式設計。 -
JPA 或 Hibernate 風格的基於註解的物件和目錄對應。
-
Spring Data 儲存庫支援,包含 QueryDSL 的支援。
-
簡化建構 LDAP 查詢和 distinguished name 的工具。
-
適當的 LDAP 連線池。
-
用戶端 LDAP 補償交易支援。
傳統 Java LDAP 與 LdapClient
的比較
考慮一個方法,該方法應在某些儲存空間中搜尋所有人員,並以清單形式傳回其姓名。透過使用 JDBC,我們將建立連線,並使用語句執行查詢。然後,我們將迴圈遍歷結果集,並檢索我們想要的欄位,將其新增至清單。
使用 JNDI 對 LDAP 資料庫進行操作時,我們將建立context,並使用搜尋篩選器執行搜尋。然後,我們將迴圈遍歷產生的命名列舉,檢索我們想要的屬性,並將其新增至清單。
在 Java LDAP 中實作此人員姓名搜尋方法的傳統方式如下例所示。請注意以粗體標記的程式碼 - 這是實際執行與該方法商業目的相關任務的程式碼。其餘的是基礎架構。
public class TraditionalPersonRepoImpl implements PersonRepo {
public List<String> getAllPersonNames() {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/dc=example,dc=com");
DirContext ctx;
try {
ctx = new InitialDirContext(env);
} catch (NamingException e) {
throw new RuntimeException(e);
}
List<String> list = new LinkedList<String>();
NamingEnumeration results = null;
try {
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
results = ctx.search("", "(objectclass=person)", controls);
while (results.hasMore()) {
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
Attribute attr = attributes.get("cn");
String cn = attr.get().toString();
list.add(cn);
}
} catch (NameNotFoundException e) {
// The base context was not found.
// Just clean up and exit.
} catch (NamingException e) {
throw new RuntimeException(e);
} finally {
if (results != null) {
try {
results.close();
} catch (Exception e) {
// Never mind this.
}
}
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
// Never mind this.
}
}
}
return list;
}
}
透過使用 Spring LDAP AttributesMapper
和 LdapClient
類別,我們可以使用以下程式碼獲得完全相同的功能
import static org.springframework.ldap.query.LdapQueryBuilder.query;
public class PersonRepoImpl implements PersonRepo {
private LdapClient ldapClient;
public void setLdapClient(LdapClient ldapClient) {
this.ldapClient = ldapClient;
}
public List<String> getAllPersonNames() {
return ldapClient.search().query(
query().where("objectclass").is("person")
).toObject((Attributes attrs) ->
attrs.get("cn").get().toString();
);
}
}
樣板程式碼的數量明顯少於傳統範例。LdapClient
搜尋方法確保建立 DirContext
實例,執行搜尋,透過使用給定的 AttributesMapper
將屬性對應到字串,將字串收集在內部清單中,最後傳回清單。它還確保正確關閉 NamingEnumeration
和 DirContext
,並處理可能發生的任何例外狀況。
自然地,由於這是一個 Spring Framework 子專案,我們使用 Spring 來設定我們的應用程式,如下所示
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ldap https://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source
url="ldap://localhost:389"
base="dc=example,dc=com"
username="cn=Manager"
password="secret" />
<bean id="ldapClient" class="org.springframework.ldap.core.LdapClient" factory-method="create">
<constructor-arg ref="contextSource" />
</bean>
<bean id="personRepo" class="com.example.repo.PersonRepoImpl">
<property name="ldapClient" ref="ldapClient" />
</bean>
</beans>
若要使用自訂 XML 命名空間來設定 Spring LDAP 元件,您需要在 XML 宣告中包含對此命名空間的參考,如前面的範例所示。 |
2.1 版本的新功能
-
#390:新增 Spring Data Hopper 支援
-
#351:新增 commons-pool2 的支援
-
#370:在 XML 命名空間中新增支援屬性佔位符
-
#392:新增文件測試支援
-
#401:新增 assertj 的切換
-
從 JIRA 遷移到 GitHub Issues
-
新增 Gitter Chat
2.0 版本的新功能
雖然在 2.0 版本中對 Spring LDAP API 進行了相當顯著的現代化改造,但已盡可能小心地確保向後相容性。使用 Spring LDAP 1.3.x 的程式碼,在大多數情況下,當您使用 2.0 函式庫時,應該能夠在沒有任何修改的情況下編譯和執行。
例外情況是少數類別已移至新的套件,以便進行一些重要的重構。移動的類別通常不是預期的公開 API 的一部分,並且遷移程序應該很順利。升級後如果找不到 Spring LDAP 類別,您應該在 IDE 中整理匯入。
您應該會遇到一些棄用警告,但也有許多其他的 API 改進。為了盡可能從 2.0 版本中獲益,建議您遠離已棄用的類別和方法,並遷移到新的、改進的 API 工具。
以下清單簡要描述了 Spring LDAP 2.0 中最重要的變更
-
Spring LDAP 現在需要 Java 6。從 2.0 及更高版本開始的 Spring 版本仍然受到支援。
-
核心 API 已使用 Java 5+ 功能(例如泛型和可變參數)進行更新。因此,整個
spring-ldap-tiger
模組已被棄用,我們鼓勵您遷移到使用核心 Spring LDAP 類別。核心介面的參數化會導致現有程式碼出現大量編譯警告,我們鼓勵您採取適當的措施來消除這些警告。 -
ODM(物件-目錄對應)功能已移至核心,並且
LdapOperations
和LdapTemplate
中有新的方法,這些方法使用此自動轉換來往返 ODM 註解的類別。有關更多資訊,請參閱 物件-目錄對應 (ODM)。 -
現在(終於)提供了自訂 XML 命名空間,以簡化 Spring LDAP 的組態。有關更多資訊,請參閱 [組態]。
-
Spring LDAP 現在提供 Spring Data Repository 和 QueryDSL 的支援。有關更多資訊,請參閱 Spring LDAP 儲存庫。
-
在
DirContextAdapter
和 ODM 中,現在可以正確處理作為屬性值的Name
實例,關於 distinguished name 相等性。有關更多資訊,請參閱DirContextAdapter
和 Distinguished Names 作為屬性值 和 ODM 和 Distinguished Names 作為屬性值。 -
DistinguishedName
和相關類別已被棄用,改用標準 JavaLdapName
。有關如何使用LdapName
物件時函式庫提供的協助,請參閱 動態建構 Distinguished Names。 -
已新增 Fluent LDAP 查詢建構支援。這使得在使用 Spring LDAP 進行 LDAP 搜尋時,程式設計體驗更加愉快。有關 LDAP 查詢建構器支援的更多資訊,請參閱 建構 LDAP 查詢 和 進階 LDAP 查詢。
-
LdapTemplate
中的舊authenticate
方法已被棄用,改用一對新的authenticate
方法,這些方法與LdapQuery
物件一起使用,並且在驗證失敗時拋出例外狀況,使用戶更容易找出導致驗證嘗試失敗的原因。 -
範例 已經過潤飾和更新,以利用 2.0 中的功能。為了提供 LDAP 使用者管理應用程式 的有用範例,投入了相當多的精力。
套件總覽
至少,若要使用 Spring LDAP,您需要以下項目
-
spring-ldap-core
:Spring LDAP 函式庫 -
spring-core
:框架內部使用的雜項公用程式類別 -
spring-beans
:用於操作 Java Bean 的介面和類別 -
slf4j
:簡單日誌外觀模式,內部使用
除了必要的相依性之外,某些功能還需要以下選用相依性
-
spring-data-ldap
:儲存庫支援等的基本基礎架構 -
spring-context
:如果您的應用程式使用 Spring 應用程式上下文進行連接,則需要此項。spring-context
新增了應用程式物件使用一致的 API 取得資源的能力。如果您計劃使用BaseLdapPathBeanPostProcessor
,則絕對需要此項。 -
spring-tx
:如果您計劃使用用戶端補償交易支援,則需要此項。 -
spring-jdbc
:如果您計劃使用用戶端補償交易支援,則需要此項。 -
commons-pool
:如果您計劃使用連線池功能,則需要此項。 -
spring-batch
:如果您計劃將 LDIF 解析功能與 Spring Batch 一起使用,則需要此項。
spring-data-ldap 遞移地新增了 spring-repository.xsd ,而 spring-ldap.xsd 使用了它。因此,即使未使用 Spring Data 的功能集,Spring LDAP 的 XML 組態支援也需要此相依性。 |
開始使用
範例 提供了一些有關如何針對常見用例使用 Spring LDAP 的實用範例。
支援
如果您有問題,請在 Stack Overflow 上使用 spring-ldap
標籤提問。專案網頁是 spring.io/spring-ldap/。
致謝
感謝 Structure101 提供開放原始碼授權,這對於保持專案結構的檢查非常有用。