Spring Security DelegatingPasswordEncoder 不验证没有前缀的密码

Spring Security DelegatingPasswordEncoder doesn't validate passwords that have no prefix

我最近实现了 DelegatingPasswordEncoder,它根据存储密码的前缀委托给 PassworEncoders 实例。问题是,对于先前存储的没有前缀的密码,它会抛出错误 'There is no PasswordEncoder mapped for the id "null"'。有人告诉我,要定义默认的 PasswordEncoder 来验证没有前缀的密码,必须在我在下面的代码示例中指定的 DelegatingPasswordEncoder () 构造函数的第一个参数,因为存储的密码被编码为纯文本。

@Bean
    public PasswordEncoder passwordEncoder(){
      
        Map<String,PasswordEncoder> encoders= new HashMap<>();
        encoders.put("", NoOpPasswordEncoder.getInstance());
        encoders.put("noop", NoOpPasswordEncoder.getInstance());
        encoders.put("bcrypt",new BCryptPasswordEncoder());
        encoders.put("scrypt",new SCryptPasswordEncoder());
        return new DelegatingPasswordEncoder("noop",encoders);
    }

对于没有 id 的密码,您可以使用 DelegatingPasswordEncoder 方法设置默认密码编码器 setDefaultPasswordEncoderForMatches(PasswordEncoder defaultPasswordEncoderForMatches)

如果没有设置,spring默认使用UnmappedIdPasswordEncoder,抛出IllegalArgumentException