Java There is no PasswordEncoder mapped for the id "null"和Encoded password does not look like BCrypt

OAuth2密码加密

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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的人。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值