做购物网站用服务器,网站购物车作用,重庆网站维护,营销型企业网站诊断目录 一、问题 二、密码加密 1、MD5密码加密 2、BCryptPasswordEncoder加密#xff08;推荐#xff09; 2.1 特点 2.2 使用步骤 一、问题
在数据库表中的密码都是明文存储的#xff0c;安全性太低 需求#xff1a;
将密码加密后存储#xff0c;提高安全性
二、密码加密… 目录 一、问题 二、密码加密 1、MD5密码加密 2、BCryptPasswordEncoder加密推荐 2.1 特点 2.2 使用步骤 一、问题
在数据库表中的密码都是明文存储的安全性太低 需求
将密码加密后存储提高安全性
二、密码加密
1、MD5密码加密
MD5讯息摘要演算法英语MD5 Message-Digest Algorithm一种被广泛使用的密码杂凑函数可以产生出一个128位元16位元组的散列值hash value用于确保信息传输完整一致。
功能:
输入任意长度的信息经过处理输出为128位的信息 不同的输入得到的不同的结果,结果与输入都是一一对应的 可以使用Spring提供给我们的工具类DigestUtils的md5DigestAsHex方法 //密码比对使用MD5加密//对前端传过来的明文密码进行MD5加密处理password DigestUtils.md5DigestAsHex(password.getBytes());if (!password.equals(employee.getPassword())) {//密码错误throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);}
2011年后MD5加密算法可以被反推出来明文被破解已经不推荐使用
2、BCryptPasswordEncoder加密推荐
BCrypt 是一种密码散列函数即单向函数。且每次加密过后的值都不一样
2.1 特点
加密
注册用户时使用SHA-256随机盐密钥把用户输入的密码进行hash处理得到密码的hash值然后将其存入数据库中。
密码匹配
用户登录时密码匹配阶段并没有进行密码解密因为密码经过Hash处理是不可逆的而是使用相同的算法把用户输入的密码进行hash处理得到密码的hash值然后将其与从数据库中查询到的密码hash值进行比较。如果两者相同说明用户输入的密码正确
2.2 使用步骤
导入依赖仓库地址Maven Repository: org.springframework.security » spring-security-crypto (mvnrepository.com)
dependencygroupIdorg.springframework.security/groupIdartifactIdspring-security-crypto/artifactIdversion6.2.1/version
/dependency
添加加密器
在 SpringBoot 项目的配置文件中添加如下代码用于注入 BCryptPasswordEncoder 加密器
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;Configuration
public class PasswordEncoderConfiguration {Beanpublic BCryptPasswordEncoder passwordEncoder(){return new BCryptPasswordEncoder();}
}
验证密码
在需要验证密码的地方通过Autowired注解注入密码加密器然后使用 BCryptPasswordEncoder 的 matches 方法进行匹配 Autowiredprivate BCryptPasswordEncoder passwordEncoder;// 使用BCryptPasswordEncoder
// 密码比对
if (!passwordEncoder.matches(password,employee.getPassword())) {//密码错误throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
}
加密方法用于更改密码时将用户输入的明文密码进行加密
encode(明文密码)
匹配方法用于比较输入的密码加密后与数据库中已加密的密码进行比对
matches(用户输入的未加密的密码,数据库中已加密的密码)
源码 public String encode(CharSequence rawPassword) {if (rawPassword null) {throw new IllegalArgumentException(rawPassword cannot be null);} else {String salt this.getSalt();return BCrypt.hashpw(rawPassword.toString(), salt);}}public boolean matches(CharSequence rawPassword, String encodedPassword) {if (rawPassword null) {throw new IllegalArgumentException(rawPassword cannot be null);} else if (encodedPassword ! null encodedPassword.length() ! 0) {if (!this.BCRYPT_PATTERN.matcher(encodedPassword).matches()) {this.logger.warn(Encoded password does not look like BCrypt);return false;} else {return BCrypt.checkpw(rawPassword.toString(), encodedPassword);}} else {this.logger.warn(Empty encoded password);return false;}}