今日在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. 【风马一族_php】

    原文来自:http://www.cnblogs.com/sows/p/6054383.html  (博客园的)风马一族 侵犯版本,后果自负 2016-11-11   15:13:51 回顾 数组:分配 ...

  2. 【Leetcode 堆、快速选择、Top-K问题 BFPRT】有序矩阵中第K小的元素(378)

    题目 给定一个 n x n 矩阵,其中每行和每列元素均按升序排序,找到矩阵中第k小的元素. 请注意,它是排序后的第k小元素,而不是第k个元素. 示例: matrix = [ [ 1, 5, 9], [ ...

  3. 【JZOJ4928】【NOIP2017提高组模拟12.18】A

    题目描述 数据范围 对于100%的数据,n<=100000,1<=A[i]<=5000 =w= Ans=∏1ai 代码 #include<iostream> #inclu ...

  4. No.1 Verilog HDL简介

       硬件描述语言HDL(Hardware Description Language)是一种用形式化方法来描述数字电路和系统的语言.设计者利用HDL可以从抽象到具体逐层描述自己的设计思想,用一系列的分 ...

  5. poj 2236【并查集】

    poj 2236 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical t ...

  6. 《C程序设计语言》笔记(二)

    四:函数与程序结构 1:函数之间的通信可以通过参数.函数返回值以及外部变量进行. 2:如果函数定义中省略了返回值类型,则默认为int类型.如果没有函数原型,则函数将在第一次出现的表达式中被隐式声明,比 ...

  7. vue插件大全

    一.UI组件及框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI ...

  8. 2016年中国的SaaS服务商企业研究

    近年来,随着中国人口红利的消退及移动互联网红利的凸显,让中国的To C创业,尤其是O2O领域的创业经历了一波高潮.2015年末,一场"资本寒冬"让O2O创业趋于理性,但SaaS及T ...

  9. IoT SaaS加速器——助力阿尔茨海默病人护理

    场景介绍 阿尔茨海默病,是导致中老年人认知功能障碍的最常见疾病之一,是发生在老年期及老年前期的一种原发性退行性脑病.据估计,全世界痴呆症患者数量为4700万,到2030年将达到7500万人.痴呆症患者 ...

  10. @bzoj - 4382@ [POI2015] Podział naszyjnika

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 长度为 n 的一串项链,每颗珠子是 k 种颜色之一. 第 i 颗 ...