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 集成时,此特性非常有用。

相关推荐
御坂10101号7 分钟前
「2>&1」是什么意思?半个世纪的 Unix 谜题
java·数据库·bash·unix
韩立学长13 分钟前
基于Springboot校园志愿者服务平台77pz7812(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
Java基基29 分钟前
Spring让Java慢了30倍,JIT、AOT等让Java比Python快13倍,比C慢17%
java·开发语言·后端·spring
future021032 分钟前
Spring AOP核心机制:代理与拦截揭秘
java·开发语言·spring·面试·aop
qq_124987075334 分钟前
基于SpringBoot微信小程序的智能在线预约挂号系统(源码+论文+部署+安装)
spring boot·后端·微信小程序·毕业设计·计算机毕设·毕业设计源码
代码探秘者37 分钟前
【Redis】分布式锁深度解析:实现、可重入、主从一致性与强一致方案
java·数据库·redis·分布式·缓存·面试
小马爱打代码41 分钟前
SpringBoot + 异地多活 + 消息回放:金融级数据一致性容灾架构设计与演练
spring boot·金融
JAVA学习通1 小时前
InnoDB 存储引擎
java·数据库·mysql
Kim Jackson1 小时前
我的世界Java版1.21.4的Fabric模组开发教程(二十三)创建生物(下)实体在游戏中的实现(1)
java·游戏·fabric