今日在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. js将canvas保存成图片并下载

    <canvas id="canvas" width="400" height="400"></canvas> < ...

  2. TCP/IP协议学习

    计算机网路学得不好,首先先放个OSI七层网络模型吧 在协议的控制下,上层对下层进行调用,下层对上层进行服务, 上下层间用交换原语交换信息.这样可以提高传输速率,并且保证数据安全,所以说其实每一层都有存 ...

  3. js简单倒计时

    不想每次用倒计时,都现写代码,比较烦,这里记一下,也顺便分享一些倒计时简单的逻辑. 如果你有更简单方便的代码,可以分享给大家. var method = { countdownObj: { timer ...

  4. php刷新当前页面,js刷新页面

    echo "<script language=JavaScript> location.replace(location.href);</script>"; ...

  5. SPA是什么?

    认识SPA 最早单页面的应用无从知晓,在2004年,google的Gmail就使用了单页面.到了2010年,随着Backbone的问世之后,此概念才慢慢热了起来. 随着后来React.Angular. ...

  6. 2018-2-13-win10-uwp-获取按钮鼠标左键按下

    title author date CreateTime categories win10 uwp 获取按钮鼠标左键按下 lindexi 2018-2-13 17:23:3 +0800 2018-2- ...

  7. vuex之仓库数据的设置与获取

    如果你之前使用过vue.js,你一定知道在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,那么接下来我们就来学习 ...

  8. Java面向对象----封装概念

    封装 信息隐藏,隐藏对象的细节 访问修饰符 public private protected 默认 属性封装的实现 方法封装的目的(隐藏方法实现细节) package com.tanlei.newer ...

  9. WPF 线段Line过长渲染出现的问题

    原文:WPF 线段Line过长渲染出现的问题 在使用WPF的时候,可以做一个实验,在canvas里添加一条线段Line,StrokeThickness属性设置为1,然后通过放大canvas或者调整li ...

  10. 模板—LCT

    #include<iostream> #include<cstring> #include<cstdio> #define LL long long using n ...