博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
阅读量:2586 次
发布时间:2019-05-11

本文共 1788 字,大约阅读时间需要 5 分钟。

问题描述

今天在使用SpringBoot整合spring security,使用内存用户验证,但无响应报错:java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null" 

错误原因

这是因为Spring boot 2.0.3引用的security 依赖是 spring security 5.X版本,此版本需要提供一个PasswordEncorder的实例,否则后台汇报错误。

解决方式

创建一个类MyPasswordEncoder 实现PasswordEncoder接口 

package com.wang.security.config;import org.springframework.security.crypto.password.PasswordEncoder;public class MyPasswordEncoder implements PasswordEncoder {    @Override    public String encode(CharSequence charSequence) {        return charSequence.toString();    }    @Override    public boolean matches(CharSequence charSequence, String s) {        return s.equals(charSequence.toString());    }}

在使用认证的时候用MyPasswordEncoder去校验密码

@Configuration@EnableWebSecuritypublic class SpringSecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(AuthenticationManagerBuilder auth) throws Exception {        //可以设置内存指定的登录的账号密码,指定角色        //不加.passwordEncoder(new MyPasswordEncoder())        //就不是以明文的方式进行匹配,会报错        auth.inMemoryAuthentication().withUser("wang").password("123456").roles("ADMIN");        //.passwordEncoder(new MyPasswordEncoder())。        //这样,页面提交时候,密码以明文的方式进行匹配。        auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()).withUser("wang").password("123456").roles("ADMIN");    }    @Override    protected void configure(HttpSecurity http) throws Exception {       //设置登录,注销,表单登录不用拦截,其他请求要拦截        http.authorizeRequests().antMatchers("/").permitAll()                .anyRequest().authenticated()                .and()                .logout().permitAll()                .and()                .formLogin();        //关闭默认的csrf认证        http.csrf().disable();    }

 

转载于:https://www.cnblogs.com/wangxiayun/p/10304419.html

你可能感兴趣的文章
c语言对结构体排序中间变量,求助:c语言怎么实现结构体的排序? 总是弄不对啊...
查看>>
c语言宏定义只能在最前面吗,C语言宏定义注意事项
查看>>
android悬浮窗服务卡死,Android 悬浮窗兼容问题谈
查看>>
表格相关的html语言,HTML标记语言——表格标记
查看>>
web聊天界面html,PC端Web聊天界面+代码分享(HTML+CSS)
查看>>
cmake qt 添加路径 项目_CMake配置Qt工程
查看>>
使用python开发的软件协议_WEB开发——Python WSGI协议详解
查看>>
冰点下载器手机版apk_冰点文库下载器
查看>>
python信号采集代码_13行代码实现:Python实时视频采集(附源码)
查看>>
h5引入json_纯js直接引入json文件
查看>>
python格式化字符串总结_Python字符串处理方法总结
查看>>
python中true什么意思_python中的bool是什么意思
查看>>
jacobian 矩阵意义_Jacobian矩阵和Hessian矩阵的作用是什么?
查看>>
case是不是php语言关键字,PHP语言 switch 的一个注意点
查看>>
linux php mkdir失败,linux – mkdir错误:参数无效
查看>>
config.php渗透,phpMyAdmin 渗透利用总结
查看>>
java list 合并 重复的数据_Java ArrayList合并并删除重复数据3种方法
查看>>
android volley 上传图片 和参数,android - 使用android中的volley将图像上传到multipart中的服务器 - 堆栈内存溢出...
查看>>
android开发的取消清空按钮,Android开发实现带清空按钮的EditText示例
查看>>
android gp服务,ArcGIS Runtime SDK for Android开发之调用GP服务(异步调用)
查看>>