您的位置:首页 > 编程语言 > Java开发

SpringBoot整合Security登录报错 There is no PasswordEncoder mapped for the id "null"

2019-04-14 10:59 676 查看

Spring Security中密码的存储格式是“{id}…………”。前面的id是加密方式,id可以是bcrypt、sha256等,后面跟着的是加密后的密码。也就是说,程序拿到传过来的密码的时候,会首先查找被“{”和“}”包括起来的id,来确定后面的密码是被怎么样加密的,如果找不到就认为id是null。这也就是为什么我们的程序会报错:There is no PasswordEncoder mapped for the id “null”。官方文档举的例子中是各种加密方式针对同一密码加密后的存储形式,原始密码都是“password”。

如果是从内存中取出密码的话,需要在config中改一下代码

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("user1").password(new BCryptPasswordEncoder().encode("123")).roles("role1","role2","role3")
.and().withUser("user2").password(new BCryptPasswordEncoder().encode("123")).roles("role2")
.and().withUser("user3").password(new BCryptPasswordEncoder().encode("123")).roles("role3");
}

如果你用的是在数据库中存储用户名和密码,那么一般是要在用户注册时就使用BCrypt编码将用户密码加密处理后存储在数据库中。并且修改configure()方法,加入".passwordEncoder(new BCryptPasswordEncoder())",保证用户登录时使用bcrypt对密码进行处理再与数据库中的密码比对.

本文参考https://docs.spring.io/spring-security/site/docs/5.1.5.RELEASE/reference/htmlsingle/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐