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,如果想自定义处理逻辑,用默认的就不行了.此时 ...
随机推荐
- 排序函数中比较函数cmp的理解
无论是使用 sort() 或者 qsort(), 都会使用到自己定义比较函数, 习惯上定义为 cmp 如: int cmp(const void *x, const void *y) { return ...
- 在Struts2里面嵌入Spring
第一步:在web.xml中增加以下的listener <listener> <listener-class>org.springframework.web.context.Co ...
- Directx11教程(54) 简单的基于GS的billboard实现
原文:Directx11教程(54) 简单的基于GS的billboard实现 本章我们用一个billboard的实现来学习D3D11中的GS. 在VS shader中,我们输入的是顶点 ...
- jQuery纵向分类下拉菜单导航
在线演示 本地下载
- Handler, AsyncTask用法简单示例
package com.jim.testapp; import android.app.Activity; import android.os.AsyncTask; import android.os ...
- LeetCode115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard) A subse ...
- python if elif else 区别
if data_ori=='医疗': # 医疗 df = pd.read_excel(path_apply + 'apply/YS_ZY_HZSQ_样例.xls', encoding='gbk', e ...
- 模板—FFT
卷积:$C[i]=\sum \limits_{j=0}^{i}A[j]*B[i-j]$可以画图理解一下其实就是交叉相乘的和. 卷积可以看作两个多项式乘积的形式,只不过求出的结果的项数不同. FFT讲解 ...
- Pytorch - GPU ID 指定 pytorch gpu 指定
PyTorch 关于多 GPUs 时的指定使用特定 GPU. PyTorch 中的 Tensor,Variable 和 nn.Module(如 loss,layer和容器 Sequential) 等可 ...
- hdu 4114 Disney's FastPass(最短路+状态压缩)
Disney's FastPass Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...