对于最新的稳定版本,请使用 Spring Security 6.3.1! |
对于最新的稳定版本,请使用 Spring Security 6.3.1! |
Spring Security 提供了一个软件包,用于将身份验证请求委托给 Java 身份验证和授权服务 (JAAS)。 本节讨论该包。
摘要JaasAuthenticationProvider
该类是所提供的 JAAS 实现的基础。
子类必须实现创建 .
有许多可以注入其中的依赖项,如本节其余部分所述。AbstractJaasAuthenticationProvider
AuthenticationProvider
LoginContext
AbstractJaasAuthenticationProvider
JAAS CallbackHandler
大多数 JAAS 实例都需要某种回调。
这些回调通常用于从用户那里获取用户名和密码。LoginModule
在 Spring Security 部署中,Spring Security 负责此用户交互(通过身份验证机制)。
因此,当认证请求委托给JAAS时,Spring Security的认证机制已经完全填充了一个对象,该对象包含JAAS所需的所有信息。Authentication
LoginModule
因此,Spring Security 的 JAAS 包提供了两个缺省回调处理程序:和 。
这些回调处理程序中的每一个都实现 。
在大多数情况下,可以在不了解内部机制的情况下使用这些回调处理程序。JaasNameCallbackHandler
JaasPasswordCallbackHandler
JaasAuthenticationCallbackHandler
对于那些需要完全控制回调行为的用户,可以在内部用 .
是实际实现 JAAS 普通接口的类。
每当使用 JAAS 时,都会向它传递一个应用程序上下文配置实例的列表。
如果请求对实例进行回调,则回调将传递给正在包装的实例。AbstractJaasAuthenticationProvider
JaasAuthenticationCallbackHandler
InternalCallbackHandler
InternalCallbackHandler
CallbackHandler
LoginModule
InternalCallbackHandler
LoginModule
InternalCallbackHandler
JaasAuthenticationCallbackHandler
JAAS Authority授予者
JAAS 与校长合作。
甚至“角色”在 JAAS 中也表示为主体。
另一方面,Spring Security 适用于对象。
每个对象都包含一个主体和多个实例。
为了便于这些不同概念之间的映射,Spring Security 的 JAAS 软件包包含一个接口。Authentication
Authentication
GrantedAuthority
AuthorityGranter
An 负责检查 JAAS 主体并返回一组对象,这些对象表示分配给主体的权限。
对于每个返回的权限字符串,都会创建一个(实现Spring Security的接口),其中包含权限字符串和传递的JAAS主体。
通过首先使用 JAAS 成功验证用户的凭证,然后访问 it 返回的 JAAS 来获取 JAAS 主体。
进行调用,并将每个生成的主体传递给针对该属性定义的每个主体。AuthorityGranter
String
AbstractJaasAuthenticationProvider
JaasGrantedAuthority
GrantedAuthority
AuthorityGranter
AbstractJaasAuthenticationProvider
LoginModule
LoginContext
LoginContext.getSubject().getPrincipals()
AuthorityGranter
AbstractJaasAuthenticationProvider.setAuthorityGranters(List)
Spring Security 不包括任何生产实例,因为每个 JAAS 主体都具有特定于实现的含义。
但是,在单元测试中有一个演示了一个简单的实现。AuthorityGranter
TestAuthorityGranter
AuthorityGranter
默认JaasAuthenticationProvider
允许将 JAAS 对象作为依赖项注入其中。
然后,它使用注入的 JAAS 创建一个 。
这意味着它不受任何特定实现的约束。DefaultJaasAuthenticationProvider
Configuration
LoginContext
Configuration
DefaultJaasAuthenticationProvider
Configuration
JaasAuthenticationProvider
InMemory配置
为了便于将 注入 ,提供了一个名为 的默认内存中实现。
实现构造函数接受 其中每个键表示一个登录配置名称,值表示一个实例。 还支持在提供的 .
有关详细信息,请参阅 InMemoryConfiguration
的 Javadoc。Configuration
DefaultJaasAuthenticationProvider
InMemoryConfiguration
Map
Array
AppConfigurationEntry
InMemoryConfiguration
Array
AppConfigurationEntry
Map
DefaultJaasAuthenticationProvider 示例配置
虽然 的 Spring 配置可能比标准的 JAAS 配置文件更冗长,但将其与 结合使用比 更灵活,因为它不依赖于默认实现。InMemoryConfiguration
DefaultJaasAuthenticationProvider
JaasAuthenticationProvider
Configuration
下一个示例提供了使用 的配置。
请注意,自定义实现也可以很容易地注入其中。DefaultJaasAuthenticationProvider
InMemoryConfiguration
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
假定默认值是 ConfigFile
的实例。
做出此假设是为了尝试更新 .
然后使用默认值创建 .JaasAuthenticationProvider
Configuration
Configuration
JaasAuthenticationProvider
Configuration
LoginContext
假设我们有一个 JAAS 登录配置文件 ,其中包含以下内容:/WEB-INF/login.conf
JAASTest {
sample.SampleLoginModule required;
};
与所有 Spring Security Bean 一样,它是通过应用程序上下文配置的。
以下定义与上述 JAAS 登录配置文件相对应:JaasAuthenticationProvider
<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
Subject
JaasAuthenticationToken
Subject
Subject subject = Subject.getSubject(AccessController.getContext());
您可以使用 jaas-api-provision 属性配置此集成。 当与依赖于填充的 JAAS 主题的旧 API 或外部 API 集成时,此功能非常有用。