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 102 Binary Tree Level Order Traversal(DFS||BFS)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. 【Python】String 字符串

    1. split() split()通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串 split()方法语法:str.split(str="" ...

  3. 原生js实现Canvas实现拖拽式绘图,支持画笔、线条、箭头、三角形和圆形等等图形绘制功能,有实例Demo

    前言 需要用到图形绘制,没有找到完整的图形绘制实现,所以自己实现了一个 - - 演示地址:查看演示DEMO 新版本支持IE5+(你没看错,就是某软的IE浏览器)以上任意浏览器的Canvas绘图:htt ...

  4. MySQL安装过程中对The error code is 2203的解决方案

    MySQL安装过程中对The error code is 2203的解决方案 1.问题描述 Windows系统安装MySQL遇到The error code is 2203.,具体描述如下 The i ...

  5. C#中使用GetCursorPos获取屏幕坐标

    [StructLayout(LayoutKind.Sequential)] public struct POINT { public int X; public int Y; public POINT ...

  6. rt-thread的定时器管理源码分析

    1 前言 rt-thread可以采用软件定时器或硬件定时器来实现定时器管理的,所谓软件定时器是指由操作系统提供的一类系统接口,它构建在硬件定时器基础之上,使系统能够提供不受数目限制的定时器服务.而硬件 ...

  7. AI-Info-Micron-Insight:Micron 美光的技术帮助 CERN 解开宇宙奥秘

    ylbtech-AI-Info-Micron-Insight:Micron 美光的技术帮助 CERN 解开宇宙奥秘 1.返回顶部 1. Micron 美光的技术帮助 CERN 解开宇宙奥秘 大约 14 ...

  8. python3 + selenium + eclipse 中报错:'geckodriver' executable needs to be in PATH

    Windows系统解决办法如下: 1.下载geckodriver.exe: 下载地址:https://github.com/mozilla/geckodriver/releases 请根据系统版本选择 ...

  9. C++ 右值引用与移动操作

    右值引用和移动操作是C++11提出的新概念,通过这些操作,可以降低拷贝操作带来的消耗.先来简单介绍一下左值和右值. 左值一般指的是一个对象,或者说是一个持久的值,例如赋值的返回值.下标操作.解引用以及 ...

  10. 《Java多线程编程核心技术》读后感(十)

    一生产一消费:操作栈 本实例是使生产者向堆栈List对象中放入数据,使消费者从List堆栈中取出数据.List最大容量是1 package Third; import java.util.ArrayL ...