网站首页 > 技术文章 正文
There is no PasswordEncoder mapped for the id “null”异常解决办法
一. 问题描述
Spring security 5.0中新增了多种加密方式,也改变了默认的密码格式.
我们来看一下官方文档:
The general format for a password is:
{id}encodedPassword
Such that id is an identifier used to look up which PasswordEncoder should be used and encodedPassword is the original encoded password for the selected PasswordEncoder. The id must be at the beginning of the password, start with { and end with }. If the id cannot be found, the id will be null. For example, the following might be a list of passwords encoded using different id. All of the original passwords are "password".
{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
{noop}password
{pbkdf2}5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc
{scrypt}$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc=
{sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0
这段话的意思是说,现如今Spring Security中密码的存储格式是“{id}…………”.前面的id是加密方式,id可以是bcrypt、sha256等,后面跟着的是加密后的密码.也就是说,程序拿到传过来的密码的时候,会首先查找被“{”和“}”包括起来的id,来确定后面的密码是被怎么样加密的,如果找不到就认为id是null.这也就是为什么我们的程序会报错:There is no PasswordEncoder mapped for the id “null”.官方文档举的例子中是各种加密方式针对同一密码加密后的存储形式,原始密码都是“password”.
二. 解决办法
需要修改一下configure中的代码,我们要将前端传过来的密码进行某种方式加密,Spring Security 官方推荐的是使用bcrypt加密方式.
1. 内存中存取密码的修改方式
修改后是这样的:
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//inMemoryAuthentication 从内存中获取
auth
.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("user1")
.password(new BCryptPasswordEncoder()
.encode("123"))
.roles("USER");
}
inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())",这相当于登陆时用BCrypt加密方式对用户密码进行处理.以前的".password("123")" 变成了 ".password(new BCryptPasswordEncoder().encode("123"))",相当于对内存中的密码进行Bcrypt编码加密.如果比对时一致,说明密码正确,才允许登陆.
2. 在数据库中存取密码的修改方式
如果用的是在数据库中存储用户名和密码,那么一般是要在用户注册时就使用BCrypt编码将用户密码加密处理后存储在数据库中,并且修改configure()方法,加入".passwordEncoder(new BCryptPasswordEncoder())",保证用户登录时使用bcrypt对密码进行处理再与数据库中的密码比对.如下:
// 注入userDetailsService的实现类
auth.userDetailsService(userService)
.passwordEncoder(new BCryptPasswordEncoder());
猜你喜欢
- 2024-09-25 SpringBoot集成Spring Security入门体验
- 2024-09-25 Spring Boot Actuator的端点都怎么用?咱用事实说话
- 2024-09-25 Spring Security-2-表单认证 spring security关闭表单登陆
- 2024-09-25 自营性电商项目④ 自营式电商平台的主要优势在于
- 2024-09-25 微服务架构系列之–前后端分离 JWT认证机制
- 2024-09-25 SpringBoot整合SpringSecurity和JWT实现mymes认证和授权(一)
- 2024-09-25 跟我学spring security系列文章第一章 实现一个基本的登入
- 2024-09-25 SpringBoot集成Spring Security springboot集成Elasticsearch
- 2024-09-25 Spring Security身份验证详细介绍
- 2024-09-25 如何使用JWT和Spring Security保护REST API,你会多少?
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)