There is no PasswordEncoder mapped for the id "null"
配置OAuth2的时候报错,在网上寻找答案发现spring security 版本在5.0后需要对用户密码进行加密
解决办法
- 在securityConfig类下加入NoOpPasswordEncoder,不过官方已经不推荐了
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userService).passwordEncoder(NoOpPasswordEncoder.getInstance()); }
-
在securityConfig类下加入密码加密,在数据库中存的密码也是要经过这个加密的才能匹配上
@Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserService).passwordEncoder(new BCryptPasswordEncoder()); }
但在添加了密码加密之后,仍然没有解决问题,在官方文档中有一段话
-------------------------------------------------------------------------------------------------------------------
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”。
原以为是需要在用户密码前添加‘{}’格式字符串,各种尝试失败后发现是需要在客户端密码前添加该格式。
推荐阅读
-
sqlserver 不能将值NULL插入列id(列不允许有空值解决)
-
mybatis-plus的selectById(或者selectOne)在根据主键ID查询实体对象的时候偶尔会出现null的问题记录
-
关于MongoTemplate通过id查询为null的问题
-
不能将值NULL插入列'id',表'weibo.
-
There is no PasswordEncoder mapped for the id "null"
-
主键非自增列 EF 插入数据库引起的 ID 列不能为 NULL 的错误
-
主键非自增列 EF 插入数据库引起的 ID 列不能为 NULL 的错误
-
Optional long parameter 'id' is present but cannot be translated into a null value due to being decl
-
Spring Security报错:There is no PasswordEncoder mapped for the id “null“
-
spring boot使用spring boot security登陆时报错There is no PasswordEncoder mapped for the id "null"