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

相关推荐
辣香牛肉面5 分钟前
Linux下“/proc”目录的作用
java·linux·服务器
Rainbow_19917 分钟前
SpringCloud_Eureka注册中心
spring·spring cloud·eureka
skyshandianxia19 分钟前
java面试八股之MySQL怎么优化查询语句
java·mysql·面试
kussmcx27 分钟前
开始尝试从0写一个项目--后端(一)
java·spring·maven
yogima36 分钟前
在Spring Data JPA中使用@Query注解
java·数据库·spring
g323086339 分钟前
springboot封装请求参数json的源码解析
spring boot·后端·json
赫萝的红苹果40 分钟前
基于Redisson实现分布式锁
java·spring boot·分布式
wang_book1 小时前
redis学习(003 数据结构和通用命令)
java·数据库·redis·学习
英雄汉孑1 小时前
图片压缩代码和实际操作页面
java
时间瑾1 小时前
SpringMVC的视图
spring·springmvc