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

用户详细信息

UserDetailsUserDetailsService. 这DaoAuthenticationProvider验证UserDetails然后返回一个Authentication的 Principal,该 Principal 是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