Run-As 認證替換

AbstractSecurityInterceptor 能夠在安全物件回撥階段臨時替換 SecurityContextSecurityContextHolder 中的 Authentication 物件。這隻會在原始 Authentication 物件成功透過 AuthenticationManagerAccessDecisionManager 處理後發生。RunAsManager 指示在 SecurityInterceptorCallback 期間應使用的替換 Authentication 物件(如果有)。

透過在安全物件回撥階段臨時替換 Authentication 物件,受保護的呼叫可以呼叫需要不同認證和授權憑據的其他物件。它還可以針對特定的 GrantedAuthority 物件執行任何內部安全檢查。由於 Spring Security 提供了許多根據 SecurityContextHolder 的內容自動配置遠端協議的輔助類,因此在呼叫遠端 Web 服務時,這些 run-as 替換特別有用。

配置

Spring Security 提供了一個 RunAsManager 介面

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

boolean supports(ConfigAttribute attribute);

boolean supports(Class clazz);

第一個方法返回在方法呼叫期間應替換現有 Authentication 物件的 Authentication 物件。如果該方法返回 null,則表示不應進行替換。第二個方法由 AbstractSecurityInterceptor 用作其配置屬性啟動驗證的一部分。安全攔截器實現呼叫 supports(Class) 方法,以確保已配置的 RunAsManager 支援安全攔截器呈現的安全物件型別。

Spring Security 提供了一個 RunAsManager 的具體實現。如果任何 ConfigAttributeRUN_AS_ 開頭,則 RunAsManagerImpl 類返回一個替換的 RunAsUserToken。如果找到任何此類 ConfigAttribute,則替換的 RunAsUserToken 包含與原始 Authentication 物件相同的主體、憑據和授予許可權,以及每個 RUN_AS_ ConfigAttribute 的新 SimpleGrantedAuthority。每個新的 SimpleGrantedAuthority 都以 ROLE_ 為字首,後跟 RUN_AS ConfigAttribute。例如,RUN_AS_SERVER 會導致替換的 RunAsUserToken 包含 ROLE_RUN_AS_SERVER 授予許可權。

替換的 RunAsUserToken 與任何其他 Authentication 物件一樣。它需要由 AuthenticationManager 進行認證,可能透過委託給合適的 AuthenticationProvider 來完成。RunAsImplAuthenticationProvider 執行此類認證。它接受任何呈現的 RunAsUserToken 作為有效。

為確保惡意程式碼不會建立 RunAsUserToken 並將其呈現給 RunAsImplAuthenticationProvider 以保證接受,所有生成的令牌中都儲存了一個金鑰的雜湊值。RunAsManagerImplRunAsImplAuthenticationProvider 在 bean 上下文中以相同的金鑰建立

<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>

透過使用相同的金鑰,每個 RunAsUserToken 都可以被驗證,因為它是由經批准的 RunAsManagerImpl 建立的。出於安全原因,RunAsUserToken 在建立後是不可變的。

© . This site is unofficial and not affiliated with VMware.