spring security 5 There is no PasswordEncoder mapped for the id “null“ 错误

最近在学习spring security,但是在设置客户端密码时,一直出现了一下错误提示,原来没有问题的,认真上次到现在这之间的时间动了什么,我想到了我把 spring security 升级到了5.0.0,应该是这里的问题,那么总需要解决,回退也不是方法吧

仔细查找资料,发现了一下两篇资料,其实是一样的意思

https://spring.io/blog/2017/11/01/spring-security-5-0-0-rc1-released#password-storage-updated

https://docs.spring.io/spring-security/site/docs/5.0.0.RELEASE/reference/htmlsingle/#ns-password-encoder

这个是原文

Password storage has undergone a major overhaul to provide more secure defaults and the ability to migrate how passwords are stored. The default PasswordEncoder is now DelegatingPasswordEncoder which is a non-passive change. This change ensures that passwords are now encoded using BCrypt by default, allows for validating passwords in old formats, and allows for upgrading the password storage in the future.

我简单理解了一下,意思就是为了更加安全,所以就需要添加这个类型(id)

如果没有加密的时候(也即是明文保存),需要改为这样,假设明文密码是password

password -> {noop}password // noop是no operate的意思,也就是说明保存的密码没有做加密操作

总的来说就是在spring security 4的密码格式上添加了密码类型的标记,并将这个标记使用花括号包裹放在密码密文的前面,形成spring security 5 的密码格式

加密类型支持(为了方便书写,这里假设各类加密方式加密后的密码密文是password)

加密方式原来security 4的密码格式现在security 5的密码格式
bcryptpassword{bcrypt}password
ldappassword{ldap}password
MD4password{MD4}password
MD5password{MD5}password
nooppassword{noop}password
pbkdf2password{pbkdf2}password
scryptpassword{scrypt}password
SHA-1password{SHA-1}password
SHA-256password{SHA-256}password
sha256password{sha256}password

 

 

 

 

 

 

 

 

 

 

 


 接下来介绍一下分析。如果我们不添加这个类型会怎么样,我们查看 org.springframework.security.crypto.password.DelegatingPasswordEncoder 这个类,在里面一个内部类UnmappedIdPasswordEncoder的matches方法抛出了一个异常信息There is no PasswordEncoder mapped for the id \"" + id + "\"",这不就是我们抛出的异常吗?

继续看看代码,在DelegatingPasswordEncoder初始化成员变量时候 ,有这样的定义

private PasswordEncoder defaultPasswordEncoderForMatches = new UnmappedIdPasswordEncoder(); 

也就是说这里将UnmappedIdPasswordEncoder()设置为了默认,在matches函数中 

复制代码

String id = extractId(prefixEncodedPassword);
PasswordEncoder delegate = this.idToPasswordEncoder.get(id);
if(delegate == null) {
   return this.defaultPasswordEncoderForMatches
      .matches(rawPassword, prefixEncodedPassword);
}

复制代码

这里,调用了默认的matches函数,也就是UnmappedIdPasswordEncoder的matches,如果设置在数据库密码没有带{}这样的标记,或者标记里的encode id 没有在声明的id 集合内(id集合是什么?也就是上面表格中所支持的加密格式,在运行时候DelegatingPasswordEncoder的构造函数打个断点就知道了,其实就是idToPasswordEncoder这个变量的值,这个值定义PasswordEncoderFactories类中),那么它就抛出异常。

String prefixEncodedPassword) {
String id = extractId(prefixEncodedPassword);
throw new IllegalArgumentException("There is no PasswordEncoder mapped for the id \"" + id + "\"");

原文: http://www.cnblogs.com/majianming/p/7923604.html


使用版本:
spring security 5.0.0.RELEASE

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值