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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39433171/article/details/84846541

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");

完美

猜你喜欢

转载自blog.csdn.net/weixin_39433171/article/details/84846541
今日推荐