Java 身份验证和授权服务 (JAAS) 提供者
Spring Security 提供了一个包,用于将身份验证请求委托给 Java 身份验证和授权服务 (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
。每个回调处理器都实现了 JaasAuthenticationCallbackHandler
。在大多数情况下,无需了解内部机制即可使用这些回调处理器。
对于需要完全控制回调行为的用户,AbstractJaasAuthenticationProvider
在内部使用 InternalCallbackHandler
包装这些 JaasAuthenticationCallbackHandler
实例。InternalCallbackHandler
是实际实现 JAAS 标准 CallbackHandler
接口的类。任何时候使用 JAAS LoginModule
时,都会向其传递一个应用程序上下文配置的 InternalCallbackHandler
实例列表。如果 LoginModule
向 InternalCallbackHandler
实例请求回调,则回调又会传递给被包装的 JaasAuthenticationCallbackHandler
实例。
JAAS AuthorityGranter
JAAS 使用主体(principals)。即使“角色”在 JAAS 中也表示为主体。另一方面,Spring Security 使用 Authentication
对象。每个 Authentication
对象包含一个主体和多个 GrantedAuthority
实例。为了方便在这些不同概念之间进行映射,Spring Security 的 JAAS 包包含一个 AuthorityGranter
接口。
AuthorityGranter
负责检查 JAAS 主体并返回一组表示分配给该主体的权限的 String
对象。对于每个返回的权限字符串,AbstractJaasAuthenticationProvider
会创建一个 JaasGrantedAuthority
(实现了 Spring Security 的 GrantedAuthority
接口),其中包含该权限字符串和传递给 AuthorityGranter
的 JAAS 主体。AbstractJaasAuthenticationProvider
通过首先使用 JAAS LoginModule
成功验证用户凭据,然后访问其返回的 LoginContext
来获取 JAAS 主体。调用 LoginContext.getSubject().getPrincipals()
后,将每个结果主体传递给针对 AbstractJaasAuthenticationProvider.setAuthorityGranters(List)
属性定义的每个 AuthorityGranter
。
考虑到每个 JAAS 主体都有特定于实现的含义,Spring Security 不包含任何生产用的 AuthorityGranter
实例。但是,单元测试中有一个 TestAuthorityGranter
,它演示了一个简单的 AuthorityGranter
实现。
DefaultJaasAuthenticationProvider
DefaultJaasAuthenticationProvider
允许将一个 JAAS Configuration
对象作为依赖注入。然后它使用注入的 JAAS Configuration
创建一个 LoginContext
。这意味着 DefaultJaasAuthenticationProvider
不像 JaasAuthenticationProvider
那样绑定到任何特定的 Configuration
实现。
InMemoryConfiguration
为了方便将 Configuration
注入到 DefaultJaasAuthenticationProvider
中,提供了一个名为 InMemoryConfiguration
的默认内存实现。该实现的构造函数接受一个 Map
,其中每个键代表一个登录配置名称,值代表一个 AppConfigurationEntry
实例的 Array
。如果提供的 Map
中没有找到映射,InMemoryConfiguration
还支持一个默认的 AppConfigurationEntry
对象 Array
。详细信息请参见 InMemoryConfiguration
的 Javadoc。
DefaultJaasAuthenticationProvider 配置示例
虽然 InMemoryConfiguration
的 Spring 配置可能比标准的 JAAS 配置文件更冗长,但结合 DefaultJaasAuthenticationProvider
使用它比 JaasAuthenticationProvider
更灵活,因为它不依赖于默认的 Configuration
实现。
下一个示例提供了一个使用 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
的一个实例。做出此假设是为了尝试更新 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>
作为 Subject 运行
如果配置了,JaasApiIntegrationFilter
会尝试作为 JaasAuthenticationToken
上的 Subject
运行。这意味着可以使用以下方法访问 Subject
Subject subject = Subject.getSubject(AccessController.getContext());
您可以使用 jaas-api-provision 属性配置此集成。此功能在与依赖于 JAAS Subject 被填充的遗留或外部 API 集成时非常有用。