spring security (BCryptPasswordEncoder)加密及判断密码是否相同
通过BCryptPasswordEncoder的加密的相同字符串的结果是不同的,如果需要判断是否是原来的密码,需要用它自带的方法。
加密:
-
BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
-
encode.encode(password);
判断:
需要通过自带的方法 matches 将未经过加密的密码和已经过加密的密码传进去进行判断,返回布尔值。
encode.matches(oldpassword,user1.getPassword());
举例说明:
-
public class BCryptPasswordEncoderTest {
-
public static void main(String[] args) {
-
String pass = "admin";
-
BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
-
String hashPass = bcryptPasswordEncoder.encode(pass);
-
System.out.println(hashPass);
-
-
boolean f = bcryptPasswordEncoder.matches("admin",hashPass);
-
System.out.println(f);
-
-
}
-
}
可以看到,每次输出的hashPass 都不一样,
但是最终的f都为 true,即匹配成功。
查看代码,可以看到,其实每次的随机盐,都保存在hashPass中。
在进行matchs进行比较时,调用BCrypt 的String hashpw(String password, String salt)
方法。两个参数即”admin“和 hashPass
-
//******BCrypt.java******salt即取出要比较的DB中的密码*******
-
real_salt = salt.substring(off + 3, off + 25);
-
try {
-
// ***************************************************
-
passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
-
}
-
catch (UnsupportedEncodingException uee) {}
-
saltb = decode_base64(real_salt, BCRYPT_SALT_LEN);
-
B = new BCrypt();
-
hashed = B.crypt_raw(passwordb, saltb, rounds);
假定一次hashPass为:$2a$10$AxafsyVqK51p.s9WAEYWYeIY9TKEoG83LTEOSB3KUkoLtGsBKhCwe
随机盐即为 AxafsyVqK51p.s9WAEYWYe
(salt = BCrypt.gensalt();中有描述)
可见,随机盐(AxafsyVqK51p.s9WAEYWYe),会在比较的时候,重新被取出。
即,加密的hashPass中,前部分已经包含了盐信息。
</div>
原文地址:https://blog.csdn.net/qq_40741855/article/details/89358745
spring security (BCryptPasswordEncoder)加密及判断密码是否相同的更多相关文章
- spring security BCryptPasswordEncoder加密解密,不错的随机盐,不错的加密解密方法
项目中用这个加密感觉不错啊,推荐: 1.先大体看看,了解一下 浅谈使用springsecurity中的BCryptPasswordEncoder方法对密码进行加密(encode)与密码匹配(match ...
- BCryptPasswordEncoder加密及判断密码是否相同
项目中用到了BCryptPasswordEncoder对密码进行二次加密,需要注意的是,加密后的字符串比较长,数据库的长度至少为60位. 通过BCryptPasswordEncoder的加密的相同字符 ...
- spring security的BCryptPasswordEncoder加密和对密码验证的原理
目录 BCryptPasswordEncoder加密和对密码验证的原理 一.加密算法和hash算法的区别 二.源码解析 1. encode方法 2. BCrypt.hashpw方法 3. matche ...
- Spring Security 5.x兼容多种密码加密方式
1 spring security PasswordEncoder spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如: {MD5}88e2d8cd1 ...
- Spring Security 5中的默认密码编码器
1.概述 在Spring Security 4中,可以使用内存中身份验证以纯文本格式存储密码. 对版本5中的密码管理过程进行了重大改进,为密码编码和解码引入了更安全的默认机制.这意味着如果您的Spri ...
- spring boot 配置文件加密数据库用户名/密码
这篇文章为大家分享spring boot的配置文件properties文件里面使用经过加密的数据库用户名+密码,因为在自己做过的项目中,有这样的需求,尤其是一些大公司,或者说上市公司,是不会把这些敏感 ...
- spring security使用哈希加密的密码
之前我们都是使用MD5 Md5PasswordEncoder 或者SHA ShaPasswordEncoder 的哈希算法进行密码加密,在spring security中依然使用只要指定使用自定义加密 ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- Spring Security构建Rest服务-0702-个性化用户认证流程2
登录成功后的处理AuthenticationSuccessHandler: 认证成功后,默认情况下spring security会继续访问之前访问的url,如果想自定义处理逻辑,用默认的就不行了.此时 ...
随机推荐
- dialog的进度条
import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import and ...
- nginx与apache
参考链接:https://www.cnblogs.com/changning0822/p/7844004.html
- 探索云数据库最佳实践 阿里云开发者大会数据库专场邀你一起Code up!
盛夏.魔都.科技 三者在一起有什么惊喜? 7月24日,阿里云峰会·上海——开发者大会将在上海世博中心盛大启程,与未来世界的开发者们分享数据库.云原生.开源大数据等领域的技术干货,共同探讨前沿科技趋势, ...
- bzoj1579 道路升级
Description 每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i ...
- centos6.5后台进程的切换
1.运行.sh文件 直接用./sh 文件就可以运行,但是如果想后台运行,即使关闭当前的终端也可以运行的话,需要nohup命令和&命令. (1)&命令 功能:加在一个命令的最后,可以把这 ...
- @hdu - 6329@ Problem K. Transport Construction
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 n 个点,第 i 个点位于 (xi, yi). 在第 i ...
- TensorFlow的 卷积层
用 TensorFlow 做卷积 让我们用所学知识在 TensorFlow 里构建真的 CNNs.在下面的练习中,你需要设定卷积核滤波器(filters)的维度,weight,bias.这在很大程度上 ...
- web移动开发小贴士
1.判断手机类型 var u = navigator.userAgent; || u.indexOf(; //android var isiOS = !!u.match(/\(i[^;]+;( U;) ...
- Nova中的Hook机制
Nova的代码中支持Hook机制,也就是在某些函数的前后,可以加入自己的代码逻辑.Hook代码可以完全独立于Nova开发,本质上使用setuptools的entry points机制.K版本的Open ...
- iPhone:constrainedToSize获取字符串的宽高
在使用UILabel存放字符串时,经常需要获取label的长宽数据,本文列出了部分常用的计算方法. 1.获取宽度,获取字符串不折行单行显示时所需要的长度 CGSize titleSize = [aSt ...