在使用SpringBoot的security+oauth2时可能会出现这种情况,百度之后能搜出个大概,主要是security5.0以上出现的加密问题。
这时需要添加一个加密方式
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(passwordEncoder())
.withUser("admin")
.password(passwordEncoder().encode("admin"))
.roles("ADMIN");
}
指定一个加密方式,并用它将密码加密
加上这个之后我是解决了There is no PasswordEncoder mapped for the id "null"
这个bug,但是还是无法请求,会出现Encoded password does not look like BCrypt
这个bug。这是指secret
字段没有加密,需要在配置客户端信息时将这个字段加密
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client")
.authorizedGrantTypes("password")
.secret(passwordEncoder.encode("secret"))
.scopes("all");
}
收工。
当然,实际应用时不会将这两个信息存储到内存而是放到数据库中,而且也不需要指定加密方式,而是在密码前加上特定的前缀,但这都是之后的事。
前人栽树后人乘凉,希望这篇文章能够帮到刚刚使用SpringBoot的oauth2的人。