構成
Spring LDAP の設定には、カスタム XML 設定名前空間を使用することをお勧めします。これを使用するには、Bean ファイルに次のように Spring LDAP 名前空間宣言を含める必要があります。
<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">ContextSource 設定
ContextSource は、<ldap:context-source> タグを使用して定義されます。最も単純な context-source 宣言では、次のようにサーバー URL、ユーザー名、パスワードを指定する必要があります。
<ldap:context-source
username="cn=Administrator"
password="secret"
url="ldap://localhost:389" /> 前の例では、デフォルト値 (この段落の後の表を参照) と指定された URL および認証資格情報を使用して LdapContextSource を作成します。context-source で構成可能な属性は次のとおりです (* でマークされた必須属性)。
| 属性 | デフォルト | 説明 |
|---|---|---|
|
| 作成された Bean の ID。 |
| LDAP サーバーで認証するときに使用するユーザー名 (プリンシパル)。これは通常、管理ユーザーの識別名 (たとえば、 | |
| LDAP サーバーで認証するときに使用するパスワード (資格情報)。 | |
|
The URL of the LDAP server to use. The URL should be in the following format: |
|
|
| ベース DN。この属性が構成されている場合、LDAP 操作に提供され、LDAP 操作から受信されるすべての識別名は、指定された LDAP パスに関連しています。これにより、LDAP ツリーに対する作業が大幅に簡素化されます。ただし、基本パスにアクセスする必要がある場合がいくつかあります。詳細については、ベース LDAP パスへの参照の取得を参照してください。 |
|
| 匿名 (認証されていない) コンテキストを使用して読み取り専用操作を実行するかどうかを定義します。このパラメーターをカバレッジトランザクションサポートと一緒に |
|
| こちら [Oracle] (英語) で説明されているように、参照を処理するための戦略を定義します。有効な値は次のとおりです。
|
|
| ネイティブ Java LDAP 接続プーリングを使用するかどうかを指定します。代わりに Spring LDAP 接続プーリングの使用を検討してください。詳細については、プーリングのサポートを参照してください。 |
|
| 使用する |
|
| 使用する |
| 構築時に |
DirContext 認証
LDAP サーバー上で操作を実行するために DirContext インスタンスを作成する場合、これらのコンテキストは認証が必要になることがよくあります。Spring LDAP は、この設定を行うためのさまざまなオプションを提供します。
このセクションでは、LdapClient および LdapTemplate で使用する DirContext インスタンスを構築するための、ContextSource のコア機能におけるコンテキストの認証について説明します。LDAP は一般にユーザー認証のみを目的として使用され、ContextSource も同様に使用される場合があります。このプロセスについては、Spring LDAP を使用したユーザー認証で説明します。 |
デフォルトでは、読み取り専用操作と読み書き操作の両方に対して認証済みコンテキストが作成されます。context-source 要素で認証に使用する LDAP ユーザーの username および password を指定する必要があります。
username が LDAP ユーザーの識別名 (DN) である場合、context-source 要素で base LDAP パスが指定されているかどうかに関係なく、LDAP ツリーのルートからのユーザーの完全な DN である必要があります。 |
一部の LDAP サーバー設定では、匿名の読み取り専用アクセスが許可されています。読み取り専用操作に匿名コンテキストを使用する場合は、anonymous-read-only 属性を true に設定します。
カスタム DirContext 認証処理
Spring LDAP で使用されるデフォルトの認証メカニズムは SIMPLE 認証です。これは、プリンシパル(username 属性で指定)と認証情報(password で指定)が、DirContext 実装コンストラクターに送信される Hashtable に設定されることを意味します。
この処理が十分ではない場合が多くあります。たとえば、LDAP サーバーは一般に、安全な TLS チャネルでのみ通信を受け入れるように設定されています。特定の LDAP プロキシ認証メカニズムまたはその他の問題を使用する必要がある場合があります。
context-source 要素への DirContextAuthenticationStrategy 実装参照を提供することにより、代替の認証メカニズムを指定できます。これを行うには、authentication-strategy-ref 属性を設定します。
TLS
Spring LDAP provides two different configuration options for LDAP servers that require TLS secure channel communication: DefaultTlsDirContextAuthenticationStrategy and ExternalTlsDirContextAuthenticationStrategy. Both implementations negotiate a TLS channel on the target connection, but they differ in the actual authentication mechanism. Where DefaultTlsDirContextAuthenticationStrategy applies SIMPLE authentication on the secure channel (by using the specified username and password), the ExternalTlsDirContextAuthenticationStrategy uses EXTERNAL SASL authentication, applying a client certificate that is configured by using system properties for authentication.
Since different LDAP server implementations respond differently to explicit shutdown of the TLS channel (some servers require the connection be shut down gracefully, while others do not support it), the TLS DirContextAuthenticationStrategy implementations support specifying the shutdown behavior by using the shutdownTlsGracefully parameter. If this property is set to false (the default), no explicit TLS shutdown happens. If it is true, Spring LDAP tries to shut down the TLS channel gracefully before closing the target context.
When working with TLS connections, you need to make sure that the native LDAP Pooling functionality (as specified by using the native-pooling attribute) is turned off. This is particularly important if shutdownTlsGracefully is set to false. However, since the TLS channel negotiation process is quite expensive, you can gain great performance benefits by using the Spring LDAP Pooling Support, described in プーリングのサポート。 |
カスタムプリンシパルと資格情報の管理
認証された Context の作成に使用されるユーザー名 (つまり、ユーザー DN) とパスワードは、デフォルトで静的に定義されます (context-source 要素構成で定義されたものは、ContextSource の存続期間全体で使用されます)。望ましい動作ではありません。一般的なシナリオは、現在のユーザーに対して LDAP 操作を実行するときに、そのユーザーのプリンシパルと資格情報を使用する必要があるというものです。username および password を明示的に指定する代わりに、authentication-source-ref 要素を使用して、AuthenticationSource 実装への参照を context-source 要素に提供することにより、既定の動作を変更できます。認証された Context が作成されるたびに、AuthenticationSource は ContextSource によってプリンシパルと資格情報を照会されます。
Spring Security を使用する場合、Spring Security に同梱されている SpringSecurityAuthenticationSource のインスタンスを使用して ContextSource を構成することにより、現在ログインしているユーザーのプリンシパルと資格情報が常に使用されるようにすることができます。次の例は、その方法を示しています。
<beans>
...
<ldap:context-source
url="ldap://localhost:389"
authentication-source-ref="springSecurityAuthenticationSource"/>
<bean id="springSecurityAuthenticationSource"
class="org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource" />
...
</beans>username または password の指定はありません。AuthenticationSource を使用する場合は、当社の context-source を使用します。これらのプロパティは、既定の動作を使用する場合にのみ必要です。 |
SpringSecurityAuthenticationSource を使用する場合、Spring Security の LdapAuthenticationProvider を使用して LDAP に対してユーザーを認証する必要があります。 |
ネイティブ Java LDAP プーリング
内部 Java LDAP プロバイダーは、いくつかの非常に基本的なプーリング機能を提供します。AbstractContextSource で pooled フラグを使用して、この LDAP 接続プールをオンまたはオフにすることができます。デフォルト値は false (リリース 1.3 以降) です。つまり、ネイティブ Java LDAP プーリングはオフになっています。LDAP 接続プールの構成は System プロパティを使用して管理されるため、Spring コンテキスト構成の外部で手動で処理する必要があります。ネイティブプーリング構成の詳細については、こちら (英語) を参照してください。
| There are several serious deficiencies in the built-in LDAP connection pooling, which is why Spring LDAP provides a more sophisticated approach to LDAP connection pooling, described in プーリングのサポート . If you need pooling functionality, this is the recommended approach. |
プーリング構成に関係なく、ContextSource#getContext(String principal, String credentials) メソッドは常に、ネイティブ Java LDAP プーリングを明示的に使用しません。これは、パスワードのリセットができるだけ早く有効になるようにするためです。 |
LdapClient 設定
LdapClient は、LDAP バックエンドを呼び出すための新しいインターフェースです。次の方法で LdapTemplate が改善されています。
組み込みの
Streamサポートを提供しますバインド、検索、変更 (U)、バインド解除 (D)、認証を中心とした簡略化された API を提供します。
LdapClient はまだ ODM をサポートしていません。これが必要な場合は、LdapTemplate にこの容量があります。必要に応じて、LdapClient と LdapTemplate は両方とも同じアプリケーション内でうまく共存できます。 |
LdapClient は、次のように LdapClient#create ファクトリメソッドを使用して定義されます。
<bean id="ldapClient" class="org.springframework.ldap.core.LdapClient" factory-method="create">
<constructor-arg ref="contextSource" />
</bean> この要素はデフォルトの ContextSource を参照します。これは contextSource (context-source 要素のデフォルト) の ID を持つことが期待されます。
LdapClient インスタンスは、特定のチェック例外を処理する方法と、クエリに使用するデフォルトの SearchControls を設定できます。
LdapTemplate 設定
LdapTemplate は、<ldap:ldap-template> 要素を使用して定義されます。最も単純な ldap-template 宣言は、要素自体です。
<ldap:ldap-template /> この要素自体が、デフォルト ID を持つ LdapTemplate インスタンスを作成し、デフォルトの ContextSource を参照します。これは、contextSource (context-source 要素のデフォルト) の ID を持つことが期待されます。
次の表では、ldap-template で構成可能な属性について説明します。
| 属性 | デフォルト | 説明 |
|---|---|---|
|
| 作成された Bean の ID。 |
|
| 使用する |
|
| 検索のデフォルトのカウント制限。0 は無制限を意味します。 |
|
| 検索のデフォルトの制限時間 (ミリ秒)。0 は無制限を意味します。 |
|
| 検索のデフォルトの検索範囲。有効な値は次のとおりです。
|
|
| 検索で |
|
| 検索で |
| 使用する |
ベース LDAP パスへの参照の取得
前述のように、すべての操作が相対的な LDAP ツリーのルートを指定して、ベース LDAP パスを ContextSource に指定できます。これは、システム全体で相対識別名のみを使用していることを意味します。これは通常、かなり便利です。ただし、LDAP ツリーの実際のルートに関連する完全な DN を構築できるようにするために、ベースパスへのアクセスが必要になる場合があります。一例として、LDAP グループ (groupOfNames オブジェクトクラスなど) を操作する場合があります。その場合、各グループメンバーの属性値は、参照されるメンバーの完全な DN である必要があります。
For that reason, Spring LDAP has a mechanism by which any Spring-controlled bean may be supplied with the base path on startup. For beans to be notified of the base path, two things need to be in place. First, the bean that wants the base path reference needs to implement the BaseLdapNameAware interface. Second, you need to define a BaseLdapPathBeanPostProcessor in the application context. The following example shows how to implement BaseLdapNameAware:
BaseLdapNameAware の実装 public class PersonService implements PersonService, BaseLdapNameAware {
...
private LdapName basePath;
public void setBaseLdapPath(LdapName basePath) {
this.basePath = basePath;
}
...
private LdapName getFullPersonDn(Person person) {
return LdapNameBuilder.newInstance(basePath)
.add(person.getDn())
.build();
}
...
} 次の例は、BaseLdapPathBeanPostProcessor を定義する方法を示しています。
<beans>
...
<ldap:context-source
username="cn=Administrator"
password="secret"
url="ldap://localhost:389"
base="dc=261consulting,dc=com" />
...
<bean class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor" />
</beans>BaseLdapPathBeanPostProcessor のデフォルトの動作は、ApplicationContext で定義された単一の BaseLdapPathSource (AbstractContextSource) のベースパスを使用することです。複数の BaseLdapPathSource が定義されている場合は、baseLdapPathSourceName プロパティを設定して、使用するものを指定する必要があります。