随着现在对软件系统安全性要求的越来越严苛,对于很多项目,升级密码都是势在必行的,若项目使用spring security框架,只需稍作修改,便可达到更换加密方式,且自动更新存量数据的密码加密放肆。在spring security中更换密码加密方式,通常涉及迁移旧有用户密码并配置新的加密策略。以下是完整步骤和示例代码:
1. 理解spring security的加密接口
核心接口是passwordencoder,常用实现类:
bcryptpasswordencoder(推荐)scryptpasswordencoderpbkdf2passwordencoderargon2passwordencodernooppasswordencoder(明文,仅测试用)
2. 迁移方案(新旧密码共存)
使用delegatingpasswordencoder。delegatingpasswordencoder 是 spring security 中用于密码加密的核心组件,主要用于代理多种加密策略,兼容旧密码格式并支持升级加密方式,支持多种加密格式自动适配:
先来看delegatingpasswordencoder的构造方法:
public delegatingpasswordencoder(string idforencode, map<string, passwordencoder> idtopasswordencoder) {
if (idforencode == null) {
throw new illegalargumentexception("idforencode cannot be null");
} else if (!idtopasswordencoder.containskey(idforencode)) {
throw new illegalargumentexception("idforencode " + idforencode + "is not found in idtopasswordencoder " + idtopasswordencoder);
} else {
for(string id : idtopasswordencoder.keyset()) {
if (id != null) {
if (id.contains("{")) {
throw new illegalargumentexception("id " + id + " cannot contain " + "{");
}
if (id.contains("}")) {
throw new illegalargumentexception("id " + id + " cannot contain " + "}");
}
}
}
this.idforencode = idforencode;
this.passwordencoderforencode = (passwordencoder)idtopasswordencoder.get(idforencode);
this.idtopasswordencoder = new hashmap(idtopasswordencoder);
}
}
其中参数idforencode决定了密码编码器的类型,而idtopasswordencoder决定了匹配密码时的兼容的密码编码器的类型,且idtopasswordencoder中必须包含idforencode,否则新密码加密后将无法匹配。
查看delegatingpasswordencoder类的类图可知,delegatingpasswordencoder类是passwordencoder的实现类。

根据delegatingpasswordencoder的构造器,定义passwordencoder密码编码器:
@bean
public passwordencoder passwordencoder() {
string idforencode = "bcrypt"; // 默认使用bcrypt
map<string, passwordencoder> idtopasswordencoder = new hashmap<>();
idtopasswordencoder.put(idforencode, new bcryptpasswordencoder());
idtopasswordencoder.put("sha256", new messagedigestpasswordencoder("sha-256")); // 旧系统加密方式
return new delegatingpasswordencoder(idforencode, idtopasswordencoder);
}
3. 密码存储格式要求
认证时根据前缀匹配加密器:
存储格式示例:
{bcrypt}$2a$10$dxj3sw6g7p50lgmmkkmwe.20cqqubk3.hzwzg3yb1tlry.fqvm/bg
{sha256}5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8
4. 数据库密码迁移脚本
-- 将旧密码加前缀
update t_user set password = concat('{sha256}', password)
where password not like '{_}%';
5. 安全配置示例
@configuration
@enablewebsecurity
public class securityconfig extends websecurityconfigureradapter {
@override
protected void configure(authenticationmanagerbuilder auth) throws exception {
auth.jdbcauthentication()
.datasource(datasource)
.passwordencoder(passwordencoder())
.usersbyusernamequery("select username, password, enabled from users where username=?")
.authoritiesbyusernamequery("select username, authority from authorities where username=?");
}
@bean
public passwordencoder passwordencoder() {
// 委托式编码器配置
string idforencode = "bcrypt"; // 默认使用bcrypt
map<string, passwordencoder> idtopasswordencoder = new hashmap<>();
idtopasswordencoder.put(idforencode, new bcryptpasswordencoder());
idtopasswordencoder.put("sha256", new messagedigestpasswordencoder("sha-256")); // 旧系统加密方式
return new delegatingpasswordencoder(idforencode, idtopasswordencoder);
}
}
6. 密码自动升级策略(可选)
密码自动升级策略,适用于用户登录时自动更新密码格式,需要自定义passwordencoder的实现类,并在第5点安全配置中使用,可参照bcryptpasswordencoder进行实现。
public class autoupgradepasswordencoder implements passwordencoder {
private final passwordencoder currentencoder;
private final passwordencoder newencoder;
@override
public boolean matches(charsequence rawpassword, string encodedpassword) {
if (currentencoder.matches(rawpassword, encodedpassword)) {
// 匹配成功且是旧编码时,触发密码升级
if (encodedpassword.startswith("{sha256}")) {
string newpass = newencoder.encode(rawpassword);
// 调用dao更新数据库密码...
}
return true;
}
return newencoder.matches(rawpassword, encodedpassword);
}
// encode()方法需实现...
}
7. 注意事项
- 前缀不可省略:
{id}encodedpassword格式是识别关键 - 加密强度:
// 调整bcrypt强度 (默认10) new bcryptpasswordencoder(12)
- 禁用弱加密:避免使用md5/sha-1等已被破解的算法
- 密码迁移:在系统低负载时段执行数据库更新
- 测试策略:保留旧密码的用户测试账号
8. 最佳实践
主推算法:bcrypt(自适应强度抗gpu破解)
过渡方案:

安全规范:
- 密码强度要求:至少10位(字母+数字+特殊字符)
- 定期轮换:每90天提示修改密码(配合密码历史策略)
9.总结
spring security项目的密码升级主要是通过配置passwordencoder进行实现,但需要注意需要进行密码的迁移(旧密码添加前缀)。
- 密码强度要求:至少10位(字母+数字+特殊字符) - 定期轮换:每90天提示修改密码(配合密码历史策略)
通过上述设计,系统可以同时支持新旧用户认证,并逐步将旧密码升级到更安全的加密方式,整个过程对用户几乎无感知。
以上就是spring security更换加密方式的完整方案的详细内容,更多关于spring security更换加密方式的资料请关注代码网其它相关文章!
发表评论