SpringSecurity中文文档(Servlet JAAS)

Java Authentication and Authorization Service (JAAS) Provider

Spring Security 提供了一个包,用于将认证请求委托给 Java Authentication and Authorization Service (JAAS)。本节讨论该包。

AbstractJaasAuthenticationProvider

AbstractJaasAuthenticationProvider 类是提供的 JAAS AuthenticationProvider 实现的基础。子类必须实现一个创建 LoginContext 的方法。AbstractJaasAuthenticationProvider 有许多可以注入的依赖项,如下文所述。

JAAS CallbackHandler

大多数 JAASLoginModule 实例需要某种类型的回调。这些回调通常用于从用户那里获取用户名和密码。

在 Spring Security 部署中,Spring Security 负责这种用户交互(通过认证机制)。因此,到认证请求被委托给 JAAS 时,Spring Security 的认证机制已经完全填充了一个 Authentication 对象,该对象包含了 JAAS LoginModule 所需的所有信息。

因此,Spring Security 的 JAAS 包提供了两个默认的回调处理器:JaasNameCallbackHandler 和 JaasPasswordCallbackHandler。每个回调处理器都实现了 JaasAuthenticationCallbackHandler。在大多数情况下,这些回调处理器可以在不了解内部机制的情况下使用。

对于那些需要完全控制回调行为的人,AbstractJaasAuthenticationProvider 在内部使用 InternalCallbackHandler 包装这些 JaasAuthenticationCallbackHandler 实例。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 创建一个包含权限字符串和 AbstractJaasAuthenticationProvider 传递给它的 JAAS 主体信息的 JaasGrantedAuthority(实现了 Spring Security 的 GrantedAuthority 接口)。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 并不绑定到 Configuration 的任何特定实现,而 JaasAuthenticationProvider 是绑定的。

InMemoryConfiguration

为了方便将 Configuration 注入到 DefaultJaasAuthenticationProvider 中,提供了一个默认的内存实现,名为 InMemoryConfiguration。该实现接受一个 Map,其中每个键代表一个登录配置名称,值代表一个 AppConfigurationEntry 实例数组。InMemoryConfiguration 还支持一个默认的 AppConfigurationEntry 对象数组,如果提供的 Map 中没有找到映射,则使用该数组。有关详细信息,请参阅 InMemoryConfiguration 的 Javadoc。

DefaultJaasAuthenticationProvider Example Configuration

虽然 InMemoryConfiguration的 Spring 配置可能比标准 JAAS 配置文件更加详细,但是与 DefaultJaasAuthenticationProvider 结合使用它比 JaasAuthenticationProvider 更加灵活,因为它不依赖于默认的 Configuration 实现。

下一个示例提供使用 InMemoryConfiguration 的 DefaultJaasAuthenticationProvider 的配置。请注意,Configuration 的自定义实现也可以轻松地注入到 DefaultJaasAuthenticationProvider 中。

java 复制代码
<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 使用默认配置创建 LoginContext。

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

java 复制代码
JAASTest {
	sample.SampleLoginModule required;
};

与所有 Spring Security bean 一样,JaasAuthenticationProvider 是通过应用程序上下文配置的。下面的定义与上面的 JAAS 登录配置文件相对应:

java 复制代码
<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>

Running as a Subject

如果配置好,JaasApiIntegrationFilter 将尝试作为 JaasAuthenticationToken 上的 Subject 运行。这意味着可以使用以下方法访问"主题":

java 复制代码
Subject subject = Subject.getSubject(AccessController.getContext());

您可以使用 jaas-api-provision 属性配置这个集成。当与依赖于要填充的 JAAS Subject 的遗留 API 或外部 API 集成时,此特性非常有用。

相关推荐
李松桃19 小时前
python第三次作业
java·前端·python
马士兵教育20 小时前
计算机专业学生入行IT行业,编程语言如何选择?
java·开发语言·c++·人工智能·python
本妖精不是妖精20 小时前
搭建 JNI 开发环境:使用 IntelliJ IDEA 和 CLion
java
老毛肚20 小时前
uniapp-ruoyi-spring部署宝塔
java·spring·uni-app
砚边数影20 小时前
决策树实战:基于 KingbaseES 的鸢尾花分类 —— 模型可视化输出
java·数据库·决策树·机器学习·分类·金仓数据库
夕除20 小时前
js--6
java·开发语言
手握风云-20 小时前
JavaEE 进阶第十三期:Spring Ioc & DI,从会用容器到成为容器(下)
java·spring·java-ee
组合缺一20 小时前
论 AI Skills 分布式发展的必然性:从单体智能到“云端大脑”的跃迁
java·人工智能·分布式·llm·mcp·skills
砚边数影20 小时前
决策树原理(一):信息增益与特征选择 —— Java 实现 ID3 算法
java·数据库·决策树·机器学习·kingbase·数据库平替用金仓·金仓数据库
让我上个超影吧20 小时前
天机学堂——BitMap实现签到
java·数据库·spring boot·redis·spring cloud