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“问题相关推荐

  1. java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id null

    使用springboot,权限管理使用spring security,使用内存用户验证,但无响应报错: java.lang.IllegalArgumentException: There is no ...

  2. 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 ...

  3. 关于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 ...

  4. Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”

    编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...

  5. java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id “null“

    基于数据库+adminServicelmpl 的admin.getPassword 加{noop}+SecurityConfig @Bean public PasswordEncoder passwo ...

  6. 解决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 ...

  7. 解决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 ...

  8. 解决Mybatis查询错误:Mapped Statements collection does not contain value for xxx

    前言 新手在使用mybatis注解开发的时候,往往容易出现各种错误. 上述提到的"Mapped Statements collection does not contain value fo ...

  9. 解决 Out of range value adjusted for column 'ID' at row 1

    MySQL升级到5.0.17后,在执行sql语句 INSERT INTO `news` (`ID`, `Title`, `Content`) VALUES ('', '标题', '正文'); 时出现错 ...

最新文章

  1. C++程序设计之可调用对象与标准库function
  2. image.open()得到的图片是什么类型_500G图片1秒压缩成200M,这个方法也太逆天了吧!完全免费...
  3. springboot 集成 swagger 自动生成API文档
  4. matlab遗传算法m文件,matlab上安装遗传算法工具箱
  5. Android模拟器慢的解决办法
  6. 【剑指offer】_04 重建二叉树
  7. Android官方开发文档Training系列课程中文版:管理Activity的生命周期之启动一个Activity
  8. 获取css selector,selenium的css selector元素获取方式
  9. 【洛谷习题】尼克的任务
  10. ORACLE多表关联的update语句
  11. css用户界面样式(附实例、图解)
  12. dw怎么打开html模板,Dreamweaver怎样使用网页模板及修改模板
  13. 正高、正常高和大地高的区别
  14. 字节跳动实习生转正工资_字节跳动西瓜视频招聘 | 新媒体运营实习生
  15. php生产环境配置,PHP生产环境配置 - osc_ifi9q17t的个人空间 - OSCHINA - 中文开源技术交流社区...
  16. 手机浏览器java_三款最热java手机浏览器横评(组图)
  17. 链表的有序集合(java)
  18. 1143-最长公共子序列(最长公共子序列)
  19. 一键式打造DAO,M-DAO或成Web3新宠儿
  20. 命令行参数输入特殊字符

热门文章

  1. 浅谈三次数学危机——费马大定理
  2. 我的第一个Hybrid APP的心得
  3. 洛谷P1827 美国血统
  4. 10万以内数字转换为中文小写
  5. 聚乳酸(PLA)简介
  6. 动态规划:矩阵连乘问题
  7. cisco router
  8. ulimit -c unlimited生成core文件
  9. mybatis plus报错:Invalid bound statement (not found)
  10. android记账本折线图_Android自定义View - 仿支付宝月账单折线图