此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Security 6.5.3spring-doc.cadn.net.cn

密码擦除

成功进行身份验证后,安全最佳做法是从内存中擦除凭据,以防止它们暴露于潜在的内存转储攻击。ProviderManager在 Spring Security 中,通过eraseCredentials方法,应在身份验证过程完成后调用该方法。spring-doc.cadn.net.cn

最佳实践

  • 立即擦除:不再需要凭据后应立即擦除,这可以最大限度地减少凭据在内存中公开的窗口。spring-doc.cadn.net.cn

  • 自动擦除:配置ProviderManager通过设置eraseCredentialsAfterAuthenticationtrue(默认值)。spring-doc.cadn.net.cn

  • 自定义擦除策略:在自定义中实施自定义擦除策略AuthenticationManager如果默认擦除行为不满足特定的安全要求,则实现。spring-doc.cadn.net.cn

风险评估

未能正确擦除凭据可能会导致多种风险:spring-doc.cadn.net.cn

  • 内存访问攻击:攻击者可以通过缓冲区溢出攻击或内存转储等漏洞从内存中访问原始凭据。spring-doc.cadn.net.cn

  • 内部威胁:有权访问系统的恶意内部人员可能会从应用程序内存中提取凭据。spring-doc.cadn.net.cn

  • 意外暴露:在多租户环境中,内存中滞留的凭据可能会意外暴露给其他租户。spring-doc.cadn.net.cn

实现

public class CustomAuthenticationManager implements AuthenticationManager {

	@Override
	public Authentication authenticate(Authentication authenticationRequest)
			throws AuthenticationException {

		Authentication authenticationResult;
		// TODO: Perform authentication checks...

		// Erase credentials post-check
		if (authenticationResult instanceof CredentialsContainer container) {
			container.eraseCredentials();
		}
	}

}

通过实施这些做法,组织可以确保凭据不会暴露在系统内存中,从而显着增强其身份验证系统的安全性。spring-doc.cadn.net.cn