对于最新的稳定版本,请使用 Spring Security 6.5.3spring-doc.cadn.net.cn

Java 身份验证和授权服务 (JAAS) 提供程序

概述

Spring Security 提供了一个能够将身份验证请求委托给 Java 身份验证和授权服务 (JAAS) 的包。 下面将详细讨论此包。spring-doc.cadn.net.cn

抽象JaasAuthenticationProvider

AbstractJaasAuthenticationProvider是提供的 JAAS 的基础AuthenticationProvider实现。 子类必须实现一个方法,该方法创建LoginContext. 这AbstractJaasAuthenticationProvider有许多可以注入其中的依赖项,下面将讨论。spring-doc.cadn.net.cn

JAAS 回调处理程序

大多数 JAASLoginModule需要某种回调。 这些回调通常用于从用户那里获取用户名和密码。spring-doc.cadn.net.cn

在 Spring Security 部署中,Spring Security 负责此用户交互(通过身份验证机制)。 因此,当身份验证请求被委托给JAAS时,Spring Security的身份验证机制已经完全填充了Authentication包含 JAAS 要求的所有信息的对象LoginModule.spring-doc.cadn.net.cn

因此,Spring Security 的 JAAS 包提供了两个默认的回调处理程序,JaasNameCallbackHandlerJaasPasswordCallbackHandler. 这些回调处理程序中的每一个都实现JaasAuthenticationCallbackHandler. 在大多数情况下,这些回调处理程序可以在不了解内部机制的情况下简单地使用。spring-doc.cadn.net.cn

对于那些需要在内部完全控制回调行为的用户AbstractJaasAuthenticationProvider将这些JaasAuthenticationCallbackHandlers 与InternalCallbackHandler. 这InternalCallbackHandler是实际实现 JAAS normal 的类CallbackHandler接口。 任何时候 JAASLoginModule,则会传递已配置的应用程序上下文列表InternalCallbackHandlers. 如果LoginModule请求针对InternalCallbackHandlers,回调又传递给JaasAuthenticationCallbackHandlers 被包装。spring-doc.cadn.net.cn

JAAS权威授予者

JAAS 与校长合作。 甚至“角色”在 JAAS 中也表示为主体。 另一方面,Spring Security 与Authentication对象。 每Authentication对象包含单个主体,并且多个GrantedAuthoritys. 为了促进这些不同概念之间的映射,Spring Security 的 JAAS 包包括一个AuthorityGranter接口。spring-doc.cadn.net.cn

AuthorityGranter负责检查 JAAS 主体并返回一组Strings,代表分配给校长的权限。 对于每个返回的权限字符串,AbstractJaasAuthenticationProvider创建一个JaasGrantedAuthority(实现 Spring Security 的GrantedAuthority接口),其中包含权限字符串和 JAAS 主体,其中AuthorityGranter被通过了。 这AbstractJaasAuthenticationProvider首先使用 JAAS 成功验证用户的凭证来获取 JAAS 主体LoginModule,然后访问LoginContext它返回。 对LoginContext.getSubject().getPrincipals(),每个生成的主体都会传递给每个AuthorityGranter根据AbstractJaasAuthenticationProvider.setAuthorityGranters(List)财产。spring-doc.cadn.net.cn

Spring Security 不包括任何生产AuthorityGranters 给定每个 JAAS 主体都具有特定于实现的含义。 但是,有一个TestAuthorityGranter在单元测试中,演示了简单的AuthorityGranter实现。spring-doc.cadn.net.cn

默认 JaasAuthenticationProvider

DefaultJaasAuthenticationProvider允许 JAASConfiguration对象作为依赖项注入其中。 然后它会创建一个LoginContext使用注入的 JAASConfiguration. 这意味着DefaultJaasAuthenticationProvider不绑定任何特定的实现ConfigurationJaasAuthenticationProvider是。spring-doc.cadn.net.cn

内存配置

为了方便注射ConfigurationDefaultJaasAuthenticationProvider,默认内存中实现名为InMemoryConfiguration被提供。 实现构造函数接受Map其中每个键代表一个登录配置名称,该值代表一个ArrayAppConfigurationEntrys.InMemoryConfiguration还支持默认的ArrayAppConfigurationEntry如果在提供的Map. 有关详细信息,请参阅InMemoryConfiguration.spring-doc.cadn.net.cn

DefaultJaasAuthenticationProvider 示例配置

虽然 Spring 配置InMemoryConfiguration可以比标准 JAAS 配置文件更详细,将其与DefaultJaasAuthenticationProviderJaasAuthenticationProvider因为它不依赖于默认值Configuration实现。spring-doc.cadn.net.cn

配置示例DefaultJaasAuthenticationProviderInMemoryConfiguration下面提供。 请注意,自定义实现Configuration可以很容易地注射到DefaultJaasAuthenticationProvider也。spring-doc.cadn.net.cn

<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>

Jaas身份验证提供程序

JaasAuthenticationProvider假定默认值Configuration ConfigFile 的一个实例。 做出此假设是为了尝试更新Configuration. 这JaasAuthenticationProvider然后使用默认的Configuration创建LoginContext.spring-doc.cadn.net.cn

假设我们有一个 JAAS 登录配置文件/WEB-INF/login.conf,内容如下:spring-doc.cadn.net.cn

JAASTest {
	sample.SampleLoginModule required;
};

与所有 Spring Security Bean 一样,JaasAuthenticationProvider通过应用程序上下文进行配置。 以下定义将对应于上述 JAAS 登录配置文件:spring-doc.cadn.net.cn

<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将尝试以SubjectJaasAuthenticationToken. 这意味着Subject可以使用以下方式访问:spring-doc.cadn.net.cn

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

可以使用 jaas-api-provision 属性轻松配置此集成。 当与依赖于要填充的 JAAS 主题的旧 API 或外部 API 集成时,此功能非常有用。spring-doc.cadn.net.cn