最新の安定バージョンについては、Spring Security 6.3.1 を使用してください!

Java 認証および認可サービス(JAAS)プロバイダー

Spring Security は、認証リクエストを Java Authentication and Authorization Service(JAAS)に委譲するためのパッケージを提供します。このセクションでは、そのパッケージについて説明します。

AbstractJaasAuthenticationProvider

AbstractJaasAuthenticationProvider クラスは、提供されている JAAS AuthenticationProvider 実装の基礎です。サブクラスは、LoginContext を作成するメソッドを実装する必要があります。このセクションの残りの部分で説明するように、AbstractJaasAuthenticationProvider には、それに注入できるいくつかの依存関係があります。

JAAS CallbackHandler

ほとんどの JAAS LoginModule インスタンスには、ある種のコールバックが必要です。これらのコールバックは通常、ユーザーからユーザー名とパスワードを取得するために使用されます。

Spring Security デプロイでは、Spring Security が(認証メカニズムを介した)このユーザーインタラクションを担当します。認証リクエストが JAAS に委譲されるまでに、Spring Security の認証メカニズムは、JAAS LoginModule に必要なすべての情報を含む Authentication オブジェクトをすでに完全に設定しています。

Spring Security の JAAS パッケージは、JaasNameCallbackHandler と JaasPasswordCallbackHandler の 2 つのデフォルトのコールバックハンドラーを提供します。これらの各コールバックハンドラーは JaasAuthenticationCallbackHandler を実装します。ほとんどの場合、これらのコールバックハンドラーは、内部の仕組みを理解していなくても使用できます。

コールバックの動作を完全に制御する必要がある場合、AbstractJaasAuthenticationProvider はこれらの JaasAuthenticationCallbackHandler インスタンスを InternalCallbackHandler で内部的にラップします。InternalCallbackHandler は、JAAS の通常の CallbackHandler インターフェースを実際に実装するクラスです。JAAS LoginModule が使用されるときはいつでも、InternalCallbackHandler インスタンスで構成されたアプリケーションコンテキストのリストが渡されます。LoginModule が InternalCallbackHandler インスタンスに対してコールバックをリクエストした場合、そのコールバックは、ラップされている JaasAuthenticationCallbackHandler インスタンスに渡されます。

JAAS AuthorityGranter

JAAS はプリンシパルと連携します。「ロール」でさえ、JAAS ではプリンシパルとして表されます。一方、Spring Security は Authentication オブジェクトで動作します。各 Authentication オブジェクトには、単一のプリンシパルと複数の GrantedAuthority インスタンスが含まれています。これらの異なる概念間のマッピングを容易にするために、Spring Security の JAAS パッケージには AuthorityGranter インターフェースが含まれています。

AuthorityGranter は、JAAS プリンシパルをインスペクションし、プリンシパルに割り当てられた権限を表す String オブジェクトのセットを返すロールを果たします。返された権限文字列ごとに、AbstractJaasAuthenticationProvider は、権限文字列と AuthorityGranter が渡された JAAS プリンシパルを含む JaasGrantedAuthority (Spring Security の GrantedAuthority インターフェースを実装する)を作成します。AbstractJaasAuthenticationProvider は、最初に JAAS LoginModule を使用してユーザーの資格情報を正常に認証し、次にそれが返す LoginContext にアクセスすることにより、JAAS プリンシパルを取得します。LoginContext.getSubject().getPrincipals() が呼び出され、結果の各プリンシパルが AbstractJaasAuthenticationProvider.setAuthorityGranters(List) プロパティに対して定義された各 AuthorityGranter に渡されます。

すべての JAAS プリンシパルには実装固有の意味があるため、Spring Security には本番 AuthorityGranter インスタンスは含まれません。ただし、単体テストには、単純な AuthorityGranter の実装を示す TestAuthorityGranter があります。

DefaultJaasAuthenticationProvider

DefaultJaasAuthenticationProvider を使用すると、依存関係として JAAS Configuration オブジェクトを注入できます。次に、注入された JAAS Configuration を使用して LoginContext を作成します。これは、JaasAuthenticationProvider のように、DefaultJaasAuthenticationProvider が Configuration の特定の実装にバインドされていないことを意味します。

InMemoryConfiguration

Configuration を DefaultJaasAuthenticationProvider に簡単に挿入できるようにするために、InMemoryConfiguration という名前のデフォルトのメモリ内実装が提供されています。実装コンストラクターは Map を受け入れます。ここで、各キーはログイン構成名を表し、値は AppConfigurationEntry インスタンスの Array を表します。InMemoryConfiguration は、提供された Map 内にマッピングが見つからない場合に使用される AppConfigurationEntry オブジェクトのデフォルト Array もサポートします。詳細については、InMemoryConfiguration の Javadoc を参照してください。

DefaultJaasAuthenticationProvider の構成例

InMemoryConfiguration の Spring 構成は、標準の JAAS 構成ファイルよりも冗長になる可能性がありますが、DefaultJaasAuthenticationProvider と組み合わせて使用すると、デフォルトの Configuration 実装に依存しないため、JaasAuthenticationProvider よりも柔軟性があります。

次の例は、InMemoryConfiguration を使用する DefaultJaasAuthenticationProvider の構成を示しています。Configuration のカスタム実装は、DefaultJaasAuthenticationProvider にも簡単に挿入できることに注意してください。

<bean id="jaasAuthProvider"
class="org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProvider">
<property name="configuration">
<bean class="org.springframework.security.authentication.jaas.memory.InMemoryConfiguration">
<constructor-arg>
	<map>
	<!--
	SPRINGSECURITY is the default loginContextName
	for AbstractJaasAuthenticationProvider
	-->
	<entry key="SPRINGSECURITY">
	<array>
	<bean class="javax.security.auth.login.AppConfigurationEntry">
		<constructor-arg value="sample.SampleLoginModule" />
		<constructor-arg>
		<util:constant static-field=
			"javax.security.auth.login.AppConfigurationEntry$LoginModuleControlFlag.REQUIRED"/>
		</constructor-arg>
		<constructor-arg>
		<map></map>
		</constructor-arg>
		</bean>
	</array>
	</entry>
	</map>
	</constructor-arg>
</bean>
</property>
<property name="authorityGranters">
<list>
	<!-- You will need to write your own implementation of AuthorityGranter -->
	<bean class="org.springframework.security.authentication.jaas.TestAuthorityGranter"/>
</list>
</property>
</bean>

JaasAuthenticationProvider

JaasAuthenticationProvider は、デフォルトの Configuration が ConfigFile [Oracle] のインスタンスであると想定しています。この仮定は、Configuration の更新を試みるために行われます。次に、JaasAuthenticationProvider はデフォルトの Configuration を使用して LoginContext を作成します。

次の内容の JAAS ログイン構成ファイル /WEB-INF/login.conf があるとします。

JAASTest {
	sample.SampleLoginModule required;
};

すべての Spring Security Bean と同様に、JaasAuthenticationProvider はアプリケーションコンテキストを介して構成されます。次の定義は、上記の JAAS ログイン構成ファイルに対応します。

<bean id="jaasAuthenticationProvider"
class="org.springframework.security.authentication.jaas.JaasAuthenticationProvider">
<property name="loginConfig" value="/WEB-INF/login.conf"/>
<property name="loginContextName" value="JAASTest"/>
<property name="callbackHandlers">
<list>
<bean
	class="org.springframework.security.authentication.jaas.JaasNameCallbackHandler"/>
<bean
	class="org.springframework.security.authentication.jaas.JaasPasswordCallbackHandler"/>
</list>
</property>
<property name="authorityGranters">
	<list>
	<bean class="org.springframework.security.authentication.jaas.TestAuthorityGranter"/>
	</list>
</property>
</bean>

サブジェクトとして実行

構成されている場合、JaasApiIntegrationFilter は JaasAuthenticationToken で Subject として実行しようとします。これは、Subject に次を使用してアクセスできることを意味します。

Subject subject = Subject.getSubject(AccessController.getContext());

この統合は、jaas-api-provision 属性を使用して構成できます。この機能は、入力されている JAAS サブジェクトに依存するレガシー API または外部 API と統合する場合に役立ちます。