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,如果想自定义处理逻辑,用默认的就不行了.此时 ...
随机推荐
- 【难为听】weex-ui
eslint 不喜欢,但是确实规范了代码,同古代统一货币一样,有无穷的意义.故喜欢 weex-ui在项目中的使用方法 npm install -g weex-toolkit@latest weex c ...
- SpringBoot 异步 定时任务 邮件
springboot异步 一: 在 MyConfiguration.java 中开启注解 @Configuration//指明当前类是一个配置类:就是来替代之前的Spring配置文件@EnableAs ...
- vue2-vux-fitness-project
非常感谢那些无私开源的程序员,希望我也能够有能力像你们那样,开源很多很有意思的东西~~ //index.html <!DOCTYPE html> <html> <head ...
- PHPCMS快速建站系列之pc:get标签的应用
GET标签使用方式如下: {pc:get sql="SELECT * FROM phpcms_member" cache="3600" page="$ ...
- 【JZOJ4848】【GDOI2017模拟11.3】永恒的契约
题目描述 宅邸迅速的燃烧着,必须带贝蒂走出禁书库!凭着感觉,又一次直接找到禁书库的门. "你,是那个人嘛?"400年了,当初圣域建立结界时没有进入圣域,被伤了心的人工精灵贝蒂,与强 ...
- ORA-03113: end-of-file on communication channel 解决方法
今天在测试数据库中对一个表插入了大量的数据, 导致数据库卡死 hang 住, 重启数据库后报错如下: C:\Documents and Settings\davidd>sqlplus " ...
- Java练习 SDUT-2445_小学数学
小学数学 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 今年中秋节,大宝哥带着一盒月饼去看望小学数学老师.碰巧数学老师 ...
- ERROR: epmd error for host "yourhostname": timeout
安装完rabbitmq-server.重新启动时间非常长,而且报错ERROR: epmd error for host "yourhostname": timeout 原因是:主机 ...
- @codeforces - 141E@ Clearing Up
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N 个点 M 条边的图,每条为黑色或者白色. 现在让你 ...
- PHP中header头设置Cookie与内置setCookie的区别
首先声明,浏览的Cookie操作都是通过HTTP Header(俗称“Http头”) 来实现.所有的服务器与客户端之间Cookie数据传输都是通过Http请求头来操作. PHP中setCookie(函 ...