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中的默认密码编码器的更多相关文章

  1. Spring Security 5.x兼容多种密码加密方式

    1 spring security PasswordEncoder spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如: {MD5}88e2d8cd1 ...

  2. Spring Security 5中 PasswordEncoder的使用

    在最新的 Spring Security 5发布版本中, 出于安全性的考虑调整了PasswordEncoder的实现与使用策略. 1.以前常用的实现 StandardPasswordEncoder, ...

  3. spring security 3中的10个典型用法小结

    spring security 3比较庞大,但功能很强,下面小结下spring security 3中值得 注意的10个典型用法 1)多个authentication-provide可以同时使用 &l ...

  4. Spring Security框架中踢人下线技术探索

    1.背景 在某次项目的开发中,使用到了Spring Security权限框架进行后端权限开发的权限校验,底层集成Spring Session组件,非常方便的集成Redis进行分布式Session的会话 ...

  5. Oracle 11g中修改默认密码过期天数和锁定次数

    Oracle 11g中修改默认密码过期天数和锁定次数 密码过期的原因一般有两种可能: 一.由于Oracle中默认在default概要文件中设置了"PASSWORD_LIFE_TIME=180 ...

  6. 关于 Spring Security OAuth2 中 CORS 跨域问题

    CORS 是一个 W3C 标准,全称是”跨域资源共享”(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了 AJA ...

  7. 关于 Spring Security OAuth2 中 Feign 调用 Token 问题

    微服务体系中,避免不了服务之间链式调用,一般使用 Feign ,由于使用 Spring Security OAuth2 全局做了安全认证,简单的一种实现方式就是在服务提供方获得 Token 再次通过 ...

  8. mysql 中用户默认密码加密问题

    问题描述: 在mysql中 user表中新增用户默认密码为123456,但是在数据库中显示不能为明文,而mysql的默认字段不能用函数 解决方法: 用触发器 delimiter | drop trig ...

  9. Spring Boot 2 中的默认日志管理与 Logback 配置详解

    Spring Boot在所有内部日志中使用Commons Logging,但是对底层日志的实现是开放的.在Spring Boot生态中,为Java Util Logging .Log4J2 和Logb ...

随机推荐

  1. leetcode 19. Remove Nth Node From End of List(链表)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  2. 集训Day1

    雅礼集训2017Day1的题 感觉上不可做实际上还挺简单的吧 T1 区间加 区间除法向下取整 查询区间和 区间最小值 大力上线段树,把除法标记推到底,加法标记就是按照线段树的来 先拿30 然后60的数 ...

  3. ACM学习历程—HDU1584 蜘蛛牌(动态规划 && 状态压缩 || 区间DP)

    Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起 ...

  4. ACM学习历程—HDU4717 The Moving Points(模拟退火 || 三分法)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...

  5. 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)

    前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...

  6. poj1050最大矩阵和——暴力枚举

    题目:http://poj.org/problem?id=1050 s[i][j]数组记录以点(i,j)为右下角.(1,1)为左上角的子矩阵内部和: 枚举子矩阵左上角和右下角,取最大值即可. 代码如下 ...

  7. UI 界面:技术决定一切

    转自:http://www.cnblogs.com/NEOCSL/archive/2012/12/10/2811153.html 在我看来,肖恩帕克不仅仅是一位技术天才和远见卓识的移动互联网领域先锋. ...

  8. PowerDesigner 导出 Excel

    http://www.cnblogs.com/hggc/archive/2013/10/15/3369857.html

  9. JavaScript高级程序设计学习笔记第十一章--DOM扩展

    1.对 DOM 的两个主要的扩展是 Selectors API(选择符 API)和 HTML5 2.Selectors API Level 1 的核心是两个方法: querySelector()和 q ...

  10. QTableWidget笔记

    1.QTableWidget继承自QTableView. 2.头文件:QTableWidget 3.简单使用: #include "mainwindow.h" #include & ...