Security内存验证无法登陆,There is no PasswordEncoder mapped for the id "null"

本文解析了在使用Spring Security 5.0进行内存验证时遇到的常见错误:There is no Password Encoder mapped for the id 'null'。介绍了如何通过正确配置密码编码器和密码格式来解决这一问题。

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

Security进行内存验证用户的时候,代码是这么写的

auth.inMemoryAuthentication().withUser("demo").password("demo").roles("USER");//用户角色
auth.inMemoryAuthentication().withUser("ssss").password("111111").roles("SSSS");
auth.inMemoryAuthentication().withUser("dddd").password("111111").roles("DDDD");
auth.inMemoryAuthentication().withUser("ffff").password("111111").roles("FFFF");

报错如下
在这里插入图片描述
看着视频教学,老师也是这么写的,但是我的就不行,已经被视频坑过不止一次两次了,都是有剪辑的,后来发现这里有坑

**首先Spring Security5.0之后,原始密码是“password”,在Spring Security5.0中密码的存储格式是“{id}。。。”这种格式的。前面的id是加密方式,也就是说,程序拿到传过来的密码的时候,首先会以“{id}。。。”这种格式来确定后面的密码是被怎么样加密的,如果找不到就认为id是null。所以我们的程序机会一直出现这个错误:There is no PasswordEncoder mapped for the id “null”。

所以我们只需要这么些就可以解决了

auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("ceshi").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");

完美

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值