解决There is no PasswordEncoder mapped for the id “null“问题
springsecurity从4.2升级到5.0之后,做简单的登录,出现如下所示的错误:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" at org.springframework.security.crypto.password.DelegatingPasswordEncoder$UnmappedIdPasswordEncoder .matches(DelegatingPasswordEncoder.java:238) at org.springframework.security.crypto.password.DelegatingPasswordEncoder.matches(DelegatingPasswordEncoder.java:198)
根据官方文档的资料和网上解决办法,需要做一些修改。
默认情况下与4.2版本不同的是,springsecurity5.0密码加密方式采用了bcrypt的方式,而且密码直接配置在xml文件中,不光是需要使用BCryptPasswordEncoder来加密,还需要指定一个encodingId,如果不指定,就会报出如题所示的错误。
我们可以看看PasswordEncoderFactories.createDelegatingPasswordEncoder()方法:
这就是为什么说默认情况下使用的bcrypt方式加密。
知道了用什么方式,我们就可以来改进了。
1、密码不加密,和springsecurity4.2一样,使用明文密码,那就需要配置密码验证方式为noop,配置如下。
<beans:bean id="passwordEncoder" class="org.springframework.security.crypto.password.NoOpPasswordEncoder"></beans:bean>
在spring-security.xml配置文件中配置以上bean即可。
密码配置如下:
<user name="admin" password="123456" authorities="ROLE_USER"/>
这样,默认密码就不需要进行加密,原样传入,原样匹配。这种配置方式密码前面,不需要加上{noop}
2、使用默认的密码加密方式,需要改动密码,并且密码格式如下:
{bcrypt}$2a$10$rY/0dflGbwW6L1yt4RVA4OH8aocD7tvMHoChyKY/XtS4DXKr.JbTC
在通过bcrypt方式加密之后的密文,前面加上{bcrypt}。
这里另外介绍两种方式来通过bcrypt方式加密,直接上源码:
package com.xxx.security.test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;public class PasswordEncoder {public static void main(String[] args) {org.springframework.security.crypto.password.PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();String password = "123456";String password2 = encoder.encode(password);System.out.println(password2);System.out.println(encoder.matches(password, password2));BCryptPasswordEncoder encoder2 = new BCryptPasswordEncoder();String password3 = encoder2.encode(password);System.out.println(password3);System.out.println(encoder2.matches(password, password3));}
}
运行以上代码,打印结果如下:
眼尖的你,可能一下子就发现了一个小问题,通过bcrypt方式加密的密文,除了{bcrypt}外两次结果不一样,怎么会这样?
和以往加密方式不同的是,springsecurity5.0以上版本对bcrypt加密方式进行了随机数处理,所以每次产生的结果都不一样。不管是哪种方式,我们如果使用默认的加密方式,就需要在xml中配置密码为如下的样子。记得前面有{bcrypt}。
{bcrypt}$2a$10$rY/0dflGbwW6L1yt4RVA4OH8aocD7tvMHoChyKY/XtS4DXKr.JbTC
报错的意思是说我们没有指定一个encodingId,最终在encoders map中没有匹配到encodingId。
如果我们需要使用MD5方式加密,我们就需要指定如下所示的密码:
{MD5}{pwegM3ORx1OzOc4eFzVztSPdeZit0BZPWTA5JB1Juc0=}dda279a014895996e55957976ec4cd36
同理,如果我们需要像springsecurity4.2那样,密码明文,我们可以这样配置:
<user name=”admin” password=”{noop}123456″ authorities=”ROLE_USER”/>
也无需像第一种方式那样在配置文件中增加NoOpPasswordEncoder的配置了。
解决There is no PasswordEncoder mapped for the id “null“问题相关推荐
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id null
使用springboot,权限管理使用spring security,使用内存用户验证,但无响应报错: java.lang.IllegalArgumentException: There is no ...
- springboot 2.x升级后出现Spring Security – There is no PasswordEncoder mapped for the id “null”的解决方案
异常描述: java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null&q ...
- 关于Spring Security中显示There is no PasswordEncoder mapped for the id “null“的问题分析
关于Spring Security中显示There is no PasswordEncoder mapped for the id "null"的问题分析 1 Spring Sec ...
- Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”
编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null“
基于数据库+adminServicelmpl 的admin.getPassword 加{noop}+SecurityConfig @Bean public PasswordEncoder passwo ...
- 解决Spring Boot报错Mapped Statements collection already contains value for...Error while adding the mapp
解决Spring Boot报错Mapped Statements collection already contains value for...Error while adding the mapp ...
- 解决HttpClient::setHeader() must be of the type string, null given
@[TOC](解决HttpClient::setHeader() must be of the type string, null given) 问题描述 Argument 2 passed to E ...
- 解决Mybatis查询错误:Mapped Statements collection does not contain value for xxx
前言 新手在使用mybatis注解开发的时候,往往容易出现各种错误. 上述提到的"Mapped Statements collection does not contain value fo ...
- 解决 Out of range value adjusted for column 'ID' at row 1
MySQL升级到5.0.17后,在执行sql语句 INSERT INTO `news` (`ID`, `Title`, `Content`) VALUES ('', '标题', '正文'); 时出现错 ...
最新文章
- C++程序设计之可调用对象与标准库function
- image.open()得到的图片是什么类型_500G图片1秒压缩成200M,这个方法也太逆天了吧!完全免费...
- springboot 集成 swagger 自动生成API文档
- matlab遗传算法m文件,matlab上安装遗传算法工具箱
- Android模拟器慢的解决办法
- 【剑指offer】_04 重建二叉树
- Android官方开发文档Training系列课程中文版:管理Activity的生命周期之启动一个Activity
- 获取css selector,selenium的css selector元素获取方式
- 【洛谷习题】尼克的任务
- ORACLE多表关联的update语句
- css用户界面样式(附实例、图解)
- dw怎么打开html模板,Dreamweaver怎样使用网页模板及修改模板
- 正高、正常高和大地高的区别
- 字节跳动实习生转正工资_字节跳动西瓜视频招聘 | 新媒体运营实习生
- php生产环境配置,PHP生产环境配置 - osc_ifi9q17t的个人空间 - OSCHINA - 中文开源技术交流社区...
- 手机浏览器java_三款最热java手机浏览器横评(组图)
- 链表的有序集合(java)
- 1143-最长公共子序列(最长公共子序列)
- 一键式打造DAO,M-DAO或成Web3新宠儿
- 命令行参数输入特殊字符