今日在SpringBoot项目中使用 Spring Security ,登录时发现报500错,报错信息如下:

There is no PasswordEncoder mapped for the id "null"

我接着查找了前端页面上,发现密码框的name属性确实指定的是 password ,并没有出错。这是怎么回事呢?

于是就上网百度,发现这是由于Spring security5中新增加了加密方式,并把原有的spring security的密码存储格式改了。

去官网查找得知,修改后的密码存储格式为:

{id}encodedPassword

于是大概就明白了程序出错的原因: 前端传过来密码后,程序会查找被 花括号"{}"包括起来的id ,以此来确定后面的密码怎么进行加密,而我们在前面并没有按该格式进行处理,这就导致找不到id,就报错了。

明白了报错的原因,就好解决了, 我们只需要对前端传过来的密码进行某种方式加密,就可以了,而官方推荐的是使用bcrypt的加密方式。解决办法如下:

在Securty配置类SecurtyConfig(继承 WebSecurityConfigurerAdapter)中修改 配置即可。


  1. @EnableWebSecurity
  2. public class SecurtyConfig extends WebSecurityConfigurerAdapter {
  3. /**
  4. * 自定义配置
  5. *
  6. * @param http
  7. * @throws Exception
  8. */
  9. @Override
  10. protected void configure(HttpSecurity http) throws Exception {
  11. http.authorizeRequests()
  12. .antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll() //都可以访问
  13. .antMatchers("/users/**").hasRole("ADMIN") //需要相应的角色才能访问
  14. .and()
  15. .formLogin() //基于Form表单登录验证
  16. .loginPage("/login").failureUrl("/login-error"); //自定义登录界面
  17. }
  18. /**
  19. * 认证信息管理
  20. * spring5中摒弃了原有的密码存储格式,官方把spring security的密码存储格式改了
  21. *
  22. * @param auth
  23. * @throws Exception
  24. */
  25. @Autowired
  26. public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  27. auth.inMemoryAuthentication() //认证信息存储到内存中
  28. .passwordEncoder(passwordEncoder())
  29. .withUser("user").password(passwordEncoder().encode("123456")).roles("ADMIN");
  30. }
  31. private PasswordEncoder passwordEncoder() {
  32. return new BCryptPasswordEncoder();
  33. }
  34. }

主要是需要对 密码进行加密操作,定义的 passwordEncoder() 方法返回一个 BCryptPasswordEncoder对象,对上面的密码进行加密。这样就解决了该问题。

另外  还有一个也可以解决.


  1. @Bean
  2. public static PasswordEncoder passwordEncoder(){
  3. return NoOpPasswordEncoder.getInstance();
  4. }

该方法已经过时,不建议使用。

原文地址:https://blog.csdn.net/m0_37564404/article/details/83378630

There is no PasswordEncoder mapped for the id "null"的解决办法的更多相关文章

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

    出现问题的原因: 内存用户验证时,Spring boot 2.0.1引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例 ...

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

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

  3. 005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id "null")

    问题描述 SpringBoot升级到了2.0之后的版本,Security也由原来的版本4升级到了5 使用WebSecurityConfigurerAdapter继承重置方法 protected voi ...

  4. There is no PasswordEncoder mapped for the id "null"

    There is no PasswordEncoder mapped for the id "null" 学习了:https://blog.csdn.net/dream_an/ar ...

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

    问题描述 今天在使用SpringBoot整合spring security,使用内存用户验证,但无响应报错:java.lang.IllegalArgumentException: There is n ...

  6. spring security 5 There is no PasswordEncoder mapped for the id "null" 错误

    转载请注明出处 http://www.cnblogs.com/majianming/p/7923604.html 最近在学习spring security,但是在设置客户端密码时,一直出现了一下错误提 ...

  7. Spring Security 报There is no PasswordEncoder mapped for the id "null"

    查了下发现是spring security 版本在5.0后就要加个PasswordEncoder了 解决办法 在securityConfig类下加入NoOpPasswordEncoder,不过官方已经 ...

  8. 单页面网站关于id冲突的解决办法

    最近做了一个单页面的网站,所有的页面加载都是通过局部刷新的方式,并且不用iframe,并且我们引入了动态tab页签: 所有的页签里的内容都只是一个元素,都在同一个html页面上,没有任何iframe分 ...

  9. Linux安装rpm包时报错Header V3 DSA/SHA1 Signature, key ID 1d1e034b: NOKEY解决办法

    这是因为yum安装了旧版本的GPG key造成的,解决办法: rpm --import /etc/pki/rpm-gpg/RPM* Header V3 DSA/SHA1 Signature, key ...

随机推荐

  1. 廖雪峰Python总结1

    1.输入输出 输入? 2.文本编辑器中,需要把Tab键自动转换为四个空格,确保不混用Tab和空格. 3.数据类型和变量 1.整数和浮点数在计算机内部存储的方式是不同的,整数运算永远是精确的(包括除法) ...

  2. linux awk命令详解,使用system来内嵌系统命令, awk合并两列

    linux awk命令详解 简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分 ...

  3. LintCode_408 二进制求和

    给定两个二进制字符串,返回他们的和(用二进制表示). 思路 string s = ""; 目标字符串 cp 存储进位;取 0或1 sum = a[i] + b[i] + cp;分为 ...

  4. oracle-600错误

    event='10841 trace name context forever' 可以屏蔽这个ORA-00600错误. SQL> show parameter event NAME TYPE V ...

  5. oracle dbms_repcat_admin能带来什么安全隐患

    如果一个用户能执行dbms_repcat_admin包,将获得极大的系统权限. 以下情况可能获得该包的执行权限: 1.在sys下grant execute on dbms_repcat_admin t ...

  6. Python学习之路1☞简介及入门代码

    在学习之前,首先了解一下python的前世今生 一.python简介与发展: python 是一种面向对象的解释性计算机程序设计语言. python由荷兰人Guido van Rossum 于1989 ...

  7. myeclipse的最有用的设置

    1 取消Myeclipse的自动文件验证(卡傻的原因)   Windows –> Perferences –>Myeclipse –> Validation,保留manual(手动) ...

  8. HZOJ Dash Speed

    测试点1-2:暴力. 测试点3-4:可以将边按r从大到小排序不断加入,然后用并茶几维护深度.好像也可以用猫树做. 好吧其他的部分分并没有看懂. 正解: 线段树分治,求出每个速度的答案. 对于速度区间$ ...

  9. 【NS2】各种TCP版本 之 TCP Tahoe 和 TCP Reno(转载)

    实验目的 学习TCP的拥塞控制机制,并了解TCP Tahoe 和 TCP Reno的运行方式. 基础知识回顾 TCP/IP (Transmission Control Protocol/Interne ...

  10. ios7.1安装提示"无法安装应用程序 由于证书无效"的解决方式二(dropbox被封项目转移到Appharbor上)

    6月18日起dropbox被天朝封了(这个真是无力吐槽),而ios7.1要求使用ssl安全连接,则须要又一次找到一个支持https的免费server. Appharbor是个不错的选择,操作简单.此外 ...