最近把spring-security-oauth2更新到最新版,发现在没有给密码加密的时候访问/oauth/token会报错:
There is no PasswordEncoder mapped for the id “null”
解决办法就是给WebSecurityConfigurerAdapter实现类的configureGlobal方法添加加密设置
@Autowired
private MyUserDetailsService userDetailsService;
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
//配置全局设置
@Autowired
private void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//设置UserDetailsService以及密码规则
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
这样就解决上面那个报错了。
最后分享一个非常杀千刀的失误,因为原本不是用这个BCryptPasswordEncoder做的加密,于是在测试类中把全部用户的密码都加密更新了一遍,可能是手滑运行了两次,导致加密了两次,然后死活登录不上。