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. POJ3417Network(LCA+树上查分||树剖+线段树)

    Yixght is a manager of the company called SzqNetwork(SN). Now she's very worried because she has jus ...

  2. python之系统编程 --进程

    1.调试(PDB) 代码: [root@master gaoji]# vim test2.py 1 #!/usr/local/bin/python3 2 # -*- coding:utf-8 -*- ...

  3. AtCoder Beginner Contest 106 2018/08/18

    A - Garden Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement There is a ...

  4. shell 脚本学习之一 ------let,echo,变量

    转自:http://blog.chinaunix.net/uid-26827001-id-3154024.html 首先看到好多以这样开头的    #!/bin/bash   这个是告诉系统 解释此脚 ...

  5. FormsAuthentication.Signout不能清楚服务器端cookie

    具体信息可以参考文章 https://support.microsoft.com/en-us/help/900111/the-formsauthentication.signout-method-do ...

  6. C/C++ Sleep(0)

    Sleep(0) 的意义是放弃当前线程执行的时间片,把自身放到等待队列之中.这时其它的线程就会得到时间片进行程序的程序.Sleep(0)能够降低当前线程的执行速 度,比如:现在系统中有100个线程(先 ...

  7. Guid.NewGuid().ToString()的几种格式 (转)

    1.Guid.NewGuid().ToString("N") 结果为:      38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid() ...

  8. Razor的主版页面框架

    类似于2.0版本中的MasterPage主版页面框架,不过mvc3.0推出的RazorView内建的主版页面语法与原本的webFormview的MasterPage相差甚远   1,Razor的页面执 ...

  9. 在SQL中直接把查询结果转换为JSON数据

    下面这篇中,已经有准备一些数据: <MS SQL server对象类型type>https://www.cnblogs.com/insus/p/10903916.html 为前端服务,直接 ...

  10. 深入剖析ASP.NET Core2.1部署模型,你会大吃一惊

    ----------------------------   以下内容针对 ASP.NET Core2.1版本,2.2推出windows IIS进程内寄宿 暂不展开讨论---------------- ...