Spring Security 5中的默认密码编码器
1.概述
在Spring Security 4中,可以使用内存中身份验证以纯文本格式存储密码。
对版本5中的密码管理过程进行了重大改进,为密码编码和解码引入了更安全的默认机制。这意味着如果您的Spring应用程序以纯文本格式存储密码,升级到Spring Security 5可能会导致问题。
在这个简短的教程中,我们将描述其中一个潜在的问题,并展示该问题的解决方案。
2. Spring Security 4
我们首先展示一个标准的安全配置,它提供简单的内存中身份验证(适用于Spring 4):
@Configuration
public class InMemoryAuthWebSecurityConfigurer
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication()
.withUser("spring")
.password("secret")
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/private/**")
.authenticated()
.antMatchers("/public/**")
.permitAll()
.and()
.httpBasic();
}
}
此配置定义所有/私有/映射方法的身份验证以及/ public /下所有内容的公共访问。
如果我们在Spring Security 5下使用相同的配置,我们会收到以下错误:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
该错误告诉我们由于没有为我们的内存中身份验证配置密码编码器,因此无法解码给定的密码。
3. Spring Security 5
我们可以通过使用PasswordEncoderFactories类定义DelegatingPasswordEncoder来解决此错误。
我们使用此编码器通过AuthenticationManagerBuilder配置我们的用户:
@Configuration
public class InMemoryAuthWebSecurityConfigurer
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
auth.inMemoryAuthentication()
.withUser("spring")
.password(encoder.encode("secret"))
.roles("USER");
}
}
现在,通过这种配置,我们使用BCrypt以以下格式存储我们的内存中密码:
{bcrypt}$2a$10$MF7hYnWLeLT66gNccBgxaONZHbrSMjlUofkp50sSpBw2PJjUqU.zS
虽然我们可以定义自己的一组密码编码器,但建议坚持使用PasswordEncoderFactories中提供的默认编码器。
3.1.迁移现有密码
我们可以通过以下方式将现有密码更新为推荐的Spring Security 5标准:
更新纯文本存储密码及其编码值:
String encoded = new BCryptPasswordEncoder().encode(plainTextPassword);
前缀散列存储的密码及其已知的编码器标识符:
{bcrypt}$2a$10$MF7hYnWLeLT66gNccBgxaONZHbrSMjlUofkp50sSpBw2PJjUqU.zS
{sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0
当存储密码的编码机制未知时,请求用户更新其密码
4.结论
在这个快速示例中,我们使用新的密码存储机制将有效的Spring 4内存中认证配置更新到Spring 5。
与往常一样,您可以在GitHub项目中找到源代码。
Spring Security 5中的默认密码编码器的更多相关文章
- Spring Security 5.x兼容多种密码加密方式
1 spring security PasswordEncoder spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如: {MD5}88e2d8cd1 ...
- Spring Security 5中 PasswordEncoder的使用
在最新的 Spring Security 5发布版本中, 出于安全性的考虑调整了PasswordEncoder的实现与使用策略. 1.以前常用的实现 StandardPasswordEncoder, ...
- spring security 3中的10个典型用法小结
spring security 3比较庞大,但功能很强,下面小结下spring security 3中值得 注意的10个典型用法 1)多个authentication-provide可以同时使用 &l ...
- Spring Security框架中踢人下线技术探索
1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...
- Oracle 11g中修改默认密码过期天数和锁定次数
Oracle 11g中修改默认密码过期天数和锁定次数 密码过期的原因一般有两种可能: 一.由于Oracle中默认在default概要文件中设置了"PASSWORD_LIFE_TIME=180 ...
- 关于 Spring Security OAuth2 中 CORS 跨域问题
CORS 是一个 W3C 标准,全称是”跨域资源共享”(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了 AJA ...
- 关于 Spring Security OAuth2 中 Feign 调用 Token 问题
微服务体系中,避免不了服务之间链式调用,一般使用 Feign ,由于使用 Spring Security OAuth2 全局做了安全认证,简单的一种实现方式就是在服务提供方获得 Token 再次通过 ...
- mysql 中用户默认密码加密问题
问题描述: 在mysql中 user表中新增用户默认密码为123456,但是在数据库中显示不能为明文,而mysql的默认字段不能用函数 解决方法: 用触发器 delimiter | drop trig ...
- Spring Boot 2 中的默认日志管理与 Logback 配置详解
Spring Boot在所有内部日志中使用Commons Logging,但是对底层日志的实现是开放的.在Spring Boot生态中,为Java Util Logging .Log4J2 和Logb ...
随机推荐
- <十八>UML核心视图动态视图之协作图
一:协作图 --->描述了对象间交互的一种模式.它通过对象之间的连接和它们相互发送的消息来显示参与交互的对象 --->协作图可以有对象和主角实例,以及描述它们之间关系和交互的连接和消息.通 ...
- 如何让Surface RT支持网站的flash
Go to the desktop version of IE10, hit the click ALT button on your keyboard, click on and then Comp ...
- 2012年浙大:Sharing
题目描述: To store English words, one method is to use linked lists and store a word letter by letter. T ...
- HDU1875(最小生成树)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Global 全局样式基本设置
1. 默认字体设置,边距设置 html { font-family: sans-serif; /* 默认字体 */ font-size: 100%; /* 在用户调整窗口大小时,字体大小做相应调整. ...
- python 抓取美丽说店铺的宝贝图片及详细信息的实现(爬虫)
对于页面的抓取,我们使用的是requests,现在大部分的网站都支持动态加载,我们在firefox f12后查找动态的url :http://www.meilishuo.com/aj/shop_lis ...
- 八 Vue学习 fetch请求
1:import {login, getAdminInfo} from '@/api/getData'(从api/getData.js中import login函数.) 看一下如下的getData.j ...
- 五 Vue学习 首页学习 (上)
首页: http://localhost:8002/#/, 登录页面如下: index.js文件中如下的路由配置,转过去看login.vue是如何实现的. const routes = [ { ...
- Windows WMIC命令使用详解2
Windows WMIC命令使用详解(附实例) https://blog.csdn.net/aflyeaglenku/article/details/77878525 第一次执行WMIC命令时,Win ...
- matlab函数 bwperim
功能:查找二值图像的边缘. 用法: BW2 = bwperim(BW1) BW2 = bwperim(BW1,conn) BW2 = bwperim(BW1,conn)表示从输入图像BW1中返回只包括 ...