之前用spring security的时候 报了这个错误 ,可以不用配置.passwordEncoder()
结果今天使用springboot security 又碰到了同样的问题 ,而且不配置也会报错 ,密码输入正确也登录失败。。
SpringBoot Security:Encoded password does not look like BCrypt
错误代码:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("123")
.roles("ADMIN");
}
正确代码:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password(new BCryptPasswordEncoder().encode("123"))
.roles("ADMIN");
}
在设置密码的时候使用BCryptPasswordEncoder()
加密就行了!
注册时同理:
account.setPassword(new BCryptPasswordEncoder().encode("123456"));
accountService.addAccount(account);
$2a$
是固定开头,10是默认strength, 可以手动设置一个[4.31]区间内的int值
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder(10);
}
还有一种过时的方法。。。
.passwordEncoder(NoOpPasswordEncoder.getInstance())
告诉security不使用加密
不过既然是划线了 就不建议使用了…
修改密码: SpringSecurity 加密后需要修改密码