There is no PasswordEncoder mapped for the id "null"的解决办法
今日在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)中修改 配置即可。
-
@EnableWebSecurity
-
public class SecurtyConfig extends WebSecurityConfigurerAdapter {
-
/**
-
* 自定义配置
-
*
-
* @param http
-
* @throws Exception
-
*/
-
@Override
-
protected void configure(HttpSecurity http) throws Exception {
-
http.authorizeRequests()
-
.antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll() //都可以访问
-
.antMatchers("/users/**").hasRole("ADMIN") //需要相应的角色才能访问
-
.and()
-
.formLogin() //基于Form表单登录验证
-
.loginPage("/login").failureUrl("/login-error"); //自定义登录界面
-
}
-
-
/**
-
* 认证信息管理
-
* spring5中摒弃了原有的密码存储格式,官方把spring security的密码存储格式改了
-
*
-
* @param auth
-
* @throws Exception
-
*/
-
@Autowired
-
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
-
auth.inMemoryAuthentication() //认证信息存储到内存中
-
.passwordEncoder(passwordEncoder())
-
.withUser("user").password(passwordEncoder().encode("123456")).roles("ADMIN");
-
}
-
-
private PasswordEncoder passwordEncoder() {
-
return new BCryptPasswordEncoder();
-
}
-
}
主要是需要对 密码进行加密操作,定义的 passwordEncoder() 方法返回一个 BCryptPasswordEncoder对象,对上面的密码进行加密。这样就解决了该问题。
另外 还有一个也可以解决.
-
@Bean
-
public static PasswordEncoder passwordEncoder(){
-
return NoOpPasswordEncoder.getInstance();
-
}
该方法已经过时,不建议使用。
原文地址:https://blog.csdn.net/m0_37564404/article/details/83378630
There is no PasswordEncoder mapped for the id "null"的解决办法的更多相关文章
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"报错
出现问题的原因: 内存用户验证时,Spring boot 2.0.1引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例 ...
- Spring Security 无法登陆,报错:There is no PasswordEncoder mapped for the id “null”
编写好继承了WebSecurityConfigurerAdapter类的WebSecurityConfig类后,我们需要在configure(AuthenticationManagerBuilder ...
- 005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id "null")
问题描述 SpringBoot升级到了2.0之后的版本,Security也由原来的版本4升级到了5 使用WebSecurityConfigurerAdapter继承重置方法 protected voi ...
- 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 ...
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
问题描述 今天在使用SpringBoot整合spring security,使用内存用户验证,但无响应报错:java.lang.IllegalArgumentException: There is n ...
- spring security 5 There is no PasswordEncoder mapped for the id "null" 错误
转载请注明出处 http://www.cnblogs.com/majianming/p/7923604.html 最近在学习spring security,但是在设置客户端密码时,一直出现了一下错误提 ...
- Spring Security 报There is no PasswordEncoder mapped for the id "null"
查了下发现是spring security 版本在5.0后就要加个PasswordEncoder了 解决办法 在securityConfig类下加入NoOpPasswordEncoder,不过官方已经 ...
- 单页面网站关于id冲突的解决办法
最近做了一个单页面的网站,所有的页面加载都是通过局部刷新的方式,并且不用iframe,并且我们引入了动态tab页签: 所有的页签里的内容都只是一个元素,都在同一个html页面上,没有任何iframe分 ...
- 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 ...
随机推荐
- MaxCompute 构建企业云数据仓库CDW的最佳实践建议
在本文中阿里云资深产品专家云郎分享了基于阿里云 MaxCompute 构建企业云数据仓库CDW的最佳实践建议. 本文内容根据演讲视频以及PPT整理而成. 大家下午好,我是云郎,之前在甲骨文做企业架构师 ...
- java reference(转)
http://blog.163.com/xubin_3@126/blog/static/112987702200962211145825/ 在Java中的引用类型,是指除了基本的变量类型之外的所有类型 ...
- python中的open函数
open函数用于文件处理 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开文件 1 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件 ...
- Python 局部变量转为全局变量
- pl/sql进阶——例外处理
在pl/sql的执行过程中发生异常时系统所作的处理称为一个例外情况(exception).通常例外情况的种类有三种: ①预定义的oracle例外情况,oracle预定义的例外情况大约有24个,对于这种 ...
- Leetcode733.Flood Fill图像渲染
有一幅以二维整数数组表示的图画,每一个整数表示该图画的像素值大小,数值在 0 到 65535 之间. 给你一个坐标 (sr, sc) 表示图像渲染开始的像素值(行 ,列)和一个新的颜色值 newCol ...
- 通过DataWorks数据集成归档日志服务数据至MaxCompute进行离线分析
通过DataWorks归档日志服务数据至MaxCompute 官方指导文档:https://help.aliyun.com/document_detail/68322.html但是会遇到大家在分区上或 ...
- 18.libgdx制作预览图,背景移动循环,改变地图颜色
经过构思,游戏将分为两部分, 1,预览图,只负责展示世界形势 2,根据预览图生成的战役项 现在要记录的是我制作预览图的部分 1.预览图只有实际地图的1/4,首先生成地图(建议不要缩放以前地图,由于误差 ...
- CSS3摆动动画效果
效果图:红包在左右摇晃 代码如下: @keyframes upAnimation { 0 % { transform: rotate(0 deg);transition - timing - func ...
- python 获取当前ip
import socket # 获取本机计算机名称 hostname = socket.gethostname() # 获取本机ip ip = socket.gethostbyname(hostnam ...