Spring Security无法登陆,报错:There is no PasswordEncoder mapped for the id "null"

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

今天在使用Spring Security进行登录验证的时候,发现报了如下错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jul 15 20:17:20 CST 2018
There was an unexpected error (type=Internal Server Error, status=500).
There is no PasswordEncoder mapped for the id "null"

我的代码是这样子写的:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
   auth.inMemoryAuthentication().withUser("Bazooka").password("123456").roles("ADMIN");
}

用户名和密码正确也无法打开资源,无论如何都登录不上去,后台一直出There is no PasswordEncoder mapped for the id "null"这个错误。

于是我去官方文档查找一下,发现问题是这个样子的:

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

要想我们的项目还能够正常登陆,需要修改一下我们的代码。

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
	auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("Bazooka").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");
}

修改代码之后,就可以正确登录了。

记录一下这个坑。

猜你喜欢

转载自blog.csdn.net/wangchengming1/article/details/81056518