权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

 

Spring Security 权限认证时无法登陆,报错:There is no PasswordEncoder mapped for the id “null”

在继承了WebSecurityConfigurerAdapter类之后,我们需要在configure(AuthenticationManagerBuilder auth) 方法中定义认证用于信息获取来源以及密码校验规则等。

此处我采用的认证信息获取来源是获取inMemoryAuthentication内存认证信息,代码如下:

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

 在登陆时发现并不能登陆,控制台报错信息如下:

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

报错的信息大概就是没有为Id 为空的PasswordEncoder,很诧异从哪个地方多出来个Id,经过多方查找发现在新的版本中,加密方式是这样的:

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

也就是说在高一点的版本当中我们需要先选择一种加密方式作为加密参数传递的一种标记。这样程序才能够识别。

然后呢我们就需要在获取内存加密信息时传入一个经过加密的对象值,此处我选择的加密方式为BCryptPasswordEncoder的加密方式。

下面就具体将整个实现过程的源码贴出来让大家有个更直观的认识:

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

这个是具体的加密过程:

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

再去看下具体的加密实现:

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”

这个最直观的实现就是会判断我们输入的密码经过加密后跟加密算法吃力过的密码是否一致。

 

最后将整个代码精简下就是这样的

权限管理框架之Spring Security 报错 There is no PasswordEncoder mapped for the id “null”