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

用户详细信息

UserDetailsUserDetailsService. 这DaoAuthenticationProvider验证UserDetails然后返回一个Authentication的主体是UserDetails由配置的UserDetailsService.spring-doc.cadn.net.cn

凭证管理

实现CredentialsContainer接口,例如扩展或实现的用户凭据的类UserDetails,强烈建议使用,尤其是在未缓存用户详细信息的应用程序中。 这种做法通过确保密码等敏感数据在内存中的保留时间不会超过必要的时间来增强安全性。spring-doc.cadn.net.cn

如果缓存了用户详细信息,请考虑创建UserDetails不包括凭据,并在自定义的响应中返回副本AuthenticationProvider而不是原始对象。 这有助于防止身份验证过程完成后应用程序的其余部分引用包含凭据的缓存实例。spring-doc.cadn.net.cn

何时实施CredentialsContainer

不采用缓存机制的应用程序UserDetails应特别考虑实施CredentialsContainer. 这种方法有助于降低与在内存中保留敏感信息相关的风险,这些信息可能容易受到内存转储等攻击媒介的攻击。spring-doc.cadn.net.cn

public class MyUserDetails implements UserDetails, CredentialsContainer {

    private String username;

    private String password;

    // UserDetails implementation...

    @Override
    public void eraseCredentials() {
        this.password = null; // Securely dereference the password field
    }

}

实施指南

  • 立即擦除:凭据在不再需要凭据后应立即删除,通常是在身份验证后删除。spring-doc.cadn.net.cn

  • 自动调用:确保eraseCredentials()由您的身份验证框架自动调用,例如AuthenticationManager,身份验证过程完成后。spring-doc.cadn.net.cn

  • 一致性:在所有应用程序中统一应用此做法,以防止可能导致数据泄露的安全漏洞。spring-doc.cadn.net.cn

超越基本接口实现

虽然像CredentialsContainer提供凭证管理框架,实际实现通常取决于特定的类及其交互。spring-doc.cadn.net.cn

例如,DaoAuthenticationProvider班级,遵守合同AuthenticationProvider,不会在其自己的authenticate方法。 相反,它依赖于ProviderManager—Spring Security 的默认实现AuthenticationManager- 处理身份验证后凭据和其他敏感数据的擦除。 这种分离强调了以下原则:AuthenticationProvider不应承担凭证管理的责任。spring-doc.cadn.net.cn

结合CredentialsContainer进入您的UserDetails实施符合安全最佳实践,通过最大限度地缩短内存中敏感数据的生命周期来减少潜在的数据泄露风险。spring-doc.cadn.net.cn