005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id "null")
问题描述
SpringBoot升级到了2.0之后的版本,Security也由原来的版本4升级到了5
使用WebSecurityConfigurerAdapter继承重置方法
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//inMemoryAuthentication 从内存中获取
auth.inMemoryAuthentication().withUser("user1").password("123456").roles("USER");
}
此时
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
在Spring Security中密码的存储格式是“{id}…………”。前面的id是加密方式,id可以是bcrypt、sha256等,后面跟着的是加密后的密码。也就是说,程序拿到传过来的密码的时候,会首先查找被“{”和“}”包括起来的id,来确定后面的密码是被怎么样加密的,如果找不到就认为id是null。这也就是为什么我们的程序会报错:There is no PasswordEncoder mapped for the id “null”。官方文档举的例子中是各种加密方式针对同一密码加密后的存储形式,原始密码都是“password”。
{bcrypt}$2a$10$dXJ3SW6G7P50lGmMkkmwe.20cQQubK3.HZWzG3YB1tlRy.fqvM/BG
{noop}password
{pbkdf2}5d923b44a6d129f3ddf3e3c8d29412723dcbde72445e8ef6bf3b508fbf17fa4ed4d6b99ca763d8dc
{scrypt}$e0801$8bWJaSu2IKSn9Z9kM+TPXfOc/9bdYSrN1oD9qfVThWEwdRTnO7re7Ei+fUZRJ68k9lTyuTeUp4of4g24hHnazw==$OAOec05+bXxvuu/1qZ6NUR+xQYvYv7BeL1QxwRpY5Pc=
{sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0
方式一、增加具体实例
内存
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//inMemoryAuthentication 从内存中获取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user1").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");
}
在inMemoryAuthentication()后面多了".passwordEncoder(new BCryptPasswordEncoder())",相当于登陆时用BCrypt加密方式对用户密码进行处理。以前的".password("123456")" 变成了 ".password(new BCryptPasswordEncoder().encode("123456"))" ,这相当于对内存中的密码进行Bcrypt编码加密。比对时一致,说明密码正确,允许登陆。
数据库
如果是注入userDetailsService
//注入userDetailsService的实现类
auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
如果你用的是在数据库中存储用户名和密码,那么一般是要在用户注册时就使用BCrypt编码将用户密码加密处理后存储在数据库中。并且修改configure()方法,加入".passwordEncoder(new BCryptPasswordEncoder())",保证用户登录时使用bcrypt对密码进行处理再与数据库中的密码比对。
没有加密方式的
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(NoOpPasswordEncoder.getInstance())
.withUser("admin").password("123456").roles("USER", "ADMIN");
}
如果配置了spring security oauth2
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory().withClient("client").secret("{noop}secret")
.authorizedGrantTypes("client_credentials", "password", "refresh_token").scopes("all");
}
005-SpringBoot2.x整合Security5(解决 There is no PasswordEncoder mapped for the id "null")的更多相关文章
- There is no PasswordEncoder mapped for the id "null"的解决办法
今日在SpringBoot项目中使用 Spring Security ,登录时发现报500错,报错信息如下: There is no PasswordEncoder mapped for the id ...
- 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ
========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...
- 消息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ 9节课
1.JMS介绍和使用场景及基础编程模型 简介:讲解什么是小写队列,JMS的基础知识和使用场景 1.什么是JMS: Java消息服务(Java Message Service),Java ...
- SpringBoot2.x 整合Spring-Session实现Session共享
SpringBoot2.x 整合Spring-Session实现Session共享 1.前言 发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务.在多个服务器 ...
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- 第二篇:SpringBoot2.0整合ActiveMQ
本篇开始将具体介绍SpringBoot如何整合其它项目. 如何创建SpringBoot项目 访问https://start.spring.io/. 依次选择构建工具Maven Project.语言ja ...
- SpringBoot2.x整合Redis实战 4节课
1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download 2.新手 ...
- SpringBoot2.0 整合 QuartJob ,实现定时器实时管理
一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...
- SpringBoot2.0 整合 Swagger2 ,构建接口管理界面
一.Swagger2简介 1.Swagger2优点 整合到Spring Boot中,构建强大RESTful API文档.省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试 ...
随机推荐
- 服务器搭建--Linux安装erlang
1.安装依赖环境: 1)安装GCC GCC-C++ Openssl等模块,安装过就不需要安装了 yum -y install make gcc gcc-c++ kernel-devel m4 ncur ...
- [转载]使用PHP模拟HTTP认证
[转载]使用PHP模拟HTTP认证 如果你希望在每个脚本的基础上实现口令保护功能,那么你可以通过结合header()函数和$PHP_AUTH_USER.$PHP_AUTH_PW全局变量的方法来创建一个 ...
- css笔记 - transition学习笔记(二)
开始把7,8月份学的css整理一下 transition过渡 1. CSS transition transition过渡 :用于当元素 从一种样式变换为另一种样式 时为元素添加效果. 2. tran ...
- bootstrap 中这段代码 使bundles 失败
_:-ms-fullscreen, :root input[type="date"], _:-ms-fullscreen, :root input[type="time& ...
- LinQ实战学习笔记(二) C#增强特性
C# 为支持LINQ添加了许多语言特性: 隐式类型局部变量 对象初始化器 Lambda表达式 扩展方法 匿名类型 了解这些新特性是全面了解LINQ的重要先解条件,因此请不要忽视它们. (一) 隐式类 ...
- Android studio Unable to start the daemon process
Unable to start the daemon process.This problem might be caused by incorrect configuration of the da ...
- G711算法学习
采样和量化 首先需要明确的两个概念,“采样”和“量化”.对于给定的一个波形,采样是从时间上将连续变成离散的过程,而采样得到的值,可能还是不能够用给定的位宽(比如8bit)来表示,这就需要经过量化,即从 ...
- -bash: locate: command not found
部分版本的linux系统使用locate快速查找某文件路径会报以下错误: -bash: locate: command not found 其原因是没有安装mlocate这个包 安装:yum -y ...
- 【CF891C】Envy 离线+最小生成树
[CF891C]Envy 题意:给你一个图,边有边权,每次询问给你一堆边,问你是否存在一个原图的最小生成树包含给出的所有边.n,m,q<=100000 题解:思路很好的题. 首先有一个非常重要的 ...
- 解决jenkins下使用HTML Publisher插件后查看html报告显示不正常
在jenkins后使用html publisher查看html报告时,发现显示不全,很多东西显示不了. 在查看官方文档后,这原来是安全问题所导致的. Jenkins安全默认是将以下功能都关闭了 1.j ...