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

jjjwad0x  于 2023-08-05  发布在  Spring
关注(0)|答案(2)|浏览(186)

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

@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);
    }

字符串

von4xj4u

von4xj4u1#

对于没有id的密码,可以使用DelegatingPasswordEncoder方法setDefaultPasswordEncoderForMatches(PasswordEncoder defaultPasswordEncoderForMatches)设置默认密码编码器
如果未设置此选项,spring默认使用UnmappedIdPasswordEncoder,从而抛出IllegalArgumentException

f87krz0w

f87krz0w2#

我相信以下也会起作用:

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

字符串

相关问题