能够在安全对象回调阶段和期间临时替换对象。 仅当 和 成功处理了原始对象时,才会发生这种情况。 表示在 期间应使用的替换对象(如果有)。AbstractSecurityInterceptorAuthenticationSecurityContextSecurityContextHolderAuthenticationAuthenticationManagerAccessDecisionManagerRunAsManagerAuthenticationSecurityInterceptorCallbackSpring中文文档

通过在安全对象回调阶段临时替换对象,安全调用可以调用需要不同身份验证和授权凭据的其他对象。 它还可以对特定对象执行任何内部安全检查。 由于 Spring Security 提供了许多帮助程序类,这些帮助程序类根据 的内容自动配置远程处理协议,因此这些运行方式替换在调用远程 Web 服务时特别有用。AuthenticationGrantedAuthoritySecurityContextHolderSpring中文文档

配置

Spring Security 提供一个接口:RunAsManagerSpring中文文档

Authentication buildRunAs(Authentication authentication, Object object,
	List<ConfigAttribute> config);

boolean supports(ConfigAttribute attribute);

boolean supports(Class clazz);

第一个方法返回在方法调用期间应替换现有对象的对象。 如果该方法返回 ,则表示不应进行替换。 第二种方法用作其配置属性的启动验证的一部分。 该方法由安全拦截器实现调用,以确保配置的内容支持安全拦截器提供的安全对象类型。AuthenticationAuthenticationnullAbstractSecurityInterceptorsupports(Class)RunAsManagerSpring中文文档

Spring Security 提供了一个具体的实现。 如果有任何以 开头的替换项,则该类将返回替换项。 如果找到任何此类内容,则替换项将包含与原始对象相同的主体、凭据和授予的权限,以及每个对象的新 . 每个 new 都以 ,后跟 . 例如,a 导致包含已授予权限的替换。RunAsManagerRunAsManagerImplRunAsUserTokenConfigAttributeRUN_AS_ConfigAttributeRunAsUserTokenAuthenticationSimpleGrantedAuthorityRUN_AS_ConfigAttributeSimpleGrantedAuthorityROLE_RUN_ASConfigAttributeRUN_AS_SERVERRunAsUserTokenROLE_RUN_AS_SERVERSpring中文文档

替换与任何其他对象一样。 它需要通过 的 进行身份验证,可能通过委托给合适的 . 执行此类身份验证。 它接受任何出示的有效。RunAsUserTokenAuthenticationAuthenticationManagerAuthenticationProviderRunAsImplAuthenticationProviderRunAsUserTokenSpring中文文档

为了确保恶意代码不会创建一个并呈现它以保证被 接受,密钥的哈希值存储在所有生成的令牌中。 and 是在 Bean 上下文中使用相同的键创建的:RunAsUserTokenRunAsImplAuthenticationProviderRunAsManagerImplRunAsImplAuthenticationProviderSpring中文文档

<bean id="runAsManager"
	class="org.springframework.security.access.intercept.RunAsManagerImpl">
<property name="key" value="my_run_as_password"/>
</bean>

<bean id="runAsAuthenticationProvider"
	class="org.springframework.security.access.intercept.RunAsImplAuthenticationProvider">
<property name="key" value="my_run_as_password"/>
</bean>

通过使用相同的密钥,可以验证每个密钥,因为它是由批准的 . 出于安全原因,创建后是不可变的。RunAsUserTokenRunAsManagerImplRunAsUserTokenSpring中文文档