通过BCryptPasswordEncoder的加密的相同字符串的结果是不同的,如果需要判断是否是原来的密码,需要用它自带的方法。

加密:

    1. BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
      1. encode.encode(password);
    1.  

    判断:

    需要通过自带的方法 matches 将未经过加密的密码和已经过加密的密码传进去进行判断,返回布尔值。

    1. encode.matches(oldpassword,user1.getPassword());

    举例说明:

      1. public class BCryptPasswordEncoderTest {
        1. public static void main(String[] args) {
          1. String pass = "admin";
            1. BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
              1. String hashPass = bcryptPasswordEncoder.encode(pass);
                1. System.out.println(hashPass);
                    1. boolean f = bcryptPasswordEncoder.matches("admin",hashPass);
                      1. System.out.println(f);
                          1. }
                            1. }
                          1.  

                          可以看到,每次输出的hashPass 都不一样,

                          但是最终的f都为 true,即匹配成功。

                          查看代码,可以看到,其实每次的随机盐,都保存在hashPass中。

                          在进行matchs进行比较时,调用BCrypt 的String hashpw(String password, String salt)

                          方法。两个参数即”admin“和 hashPass

                            1. //******BCrypt.java******salt即取出要比较的DB中的密码*******
                              1. real_salt = salt.substring(off + 3, off + 25);
                                1. try {
                                  1. // ***************************************************
                                    1. passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
                                      1. }
                                        1. catch (UnsupportedEncodingException uee) {}
                                          1. saltb = decode_base64(real_salt, BCRYPT_SALT_LEN);
                                            1. B = new BCrypt();
                                              1. hashed = B.crypt_raw(passwordb, saltb, rounds);
                                            1.  

                                            假定一次hashPass为:$2a$10$AxafsyVqK51p.s9WAEYWYeIY9TKEoG83LTEOSB3KUkoLtGsBKhCwe

                                            随机盐即为 AxafsyVqK51p.s9WAEYWYe

                                            (salt = BCrypt.gensalt();中有描述)

                                            可见,随机盐(AxafsyVqK51p.s9WAEYWYe),会在比较的时候,重新被取出。

                                            即,加密的hashPass中,前部分已经包含了盐信息。

                                            1. </div>

                                            原文地址:https://blog.csdn.net/qq_40741855/article/details/89358745

                                            spring security (BCryptPasswordEncoder)加密及判断密码是否相同的更多相关文章

                                            1. spring security BCryptPasswordEncoder加密解密,不错的随机盐,不错的加密解密方法

                                              项目中用这个加密感觉不错啊,推荐: 1.先大体看看,了解一下 浅谈使用springsecurity中的BCryptPasswordEncoder方法对密码进行加密(encode)与密码匹配(match ...

                                            2. BCryptPasswordEncoder加密及判断密码是否相同

                                              项目中用到了BCryptPasswordEncoder对密码进行二次加密,需要注意的是,加密后的字符串比较长,数据库的长度至少为60位. 通过BCryptPasswordEncoder的加密的相同字符 ...

                                            3. spring security的BCryptPasswordEncoder加密和对密码验证的原理

                                              目录 BCryptPasswordEncoder加密和对密码验证的原理 一.加密算法和hash算法的区别 二.源码解析 1. encode方法 2. BCrypt.hashpw方法 3. matche ...

                                            4. Spring Security 5.x兼容多种密码加密方式

                                              1 spring security PasswordEncoder spring security 5不需要配置密码的加密方式,而是用户密码加前缀的方式表明加密方式,如: {MD5}88e2d8cd1 ...

                                            5. Spring Security 5中的默认密码编码器

                                              1.概述 在Spring Security 4中,可以使用内存中身份验证以纯文本格式存储密码. 对版本5中的密码管理过程进行了重大改进,为密码编码和解码引入了更安全的默认机制.这意味着如果您的Spri ...

                                            6. spring boot 配置文件加密数据库用户名/密码

                                              这篇文章为大家分享spring boot的配置文件properties文件里面使用经过加密的数据库用户名+密码,因为在自己做过的项目中,有这样的需求,尤其是一些大公司,或者说上市公司,是不会把这些敏感 ...

                                            7. spring security使用哈希加密的密码

                                              之前我们都是使用MD5 Md5PasswordEncoder 或者SHA ShaPasswordEncoder 的哈希算法进行密码加密,在spring security中依然使用只要指定使用自定义加密 ...

                                            8. springboot+maven整合spring security

                                              springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...

                                            9. Spring Security构建Rest服务-0702-个性化用户认证流程2

                                              登录成功后的处理AuthenticationSuccessHandler: 认证成功后,默认情况下spring security会继续访问之前访问的url,如果想自定义处理逻辑,用默认的就不行了.此时 ...

                                            随机推荐

                                            1. 成功的背后!(送给所有IT人)

                                              希望自己迷茫的时候,看到能够惊醒 来自CSDN第3名的博主(http://blog.csdn.net/phphot/article/details/2187505) 成功的背后,有着许多不为人知的故事 ...

                                            2. 【JZOJ4742】【NOIP2016提高A组模拟9.2】单峰

                                              题目描述 输入 输出 样例输入 2 样例输出 2 数据范围 解法 答案为2^(n-1),快速幂即可. 证明:显然峰值必定为n,那么对于其他n-1个数,要么放在峰值的左边,要么放在峰值的右边,所以方案数 ...

                                            3. codevs1839 洞穴勘测

                                              题目描述 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道 ...

                                            4. LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element

                                              二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...

                                            5. Best Time to Sell and Buy Stock

                                              这道题想了很多,但是想多了.这个题思路很简单,如果当前值大于最小值,就计算差,和最大利润值比较. class Solution { public: int maxProfit(vector<in ...

                                            6. iOS开发周报-- 第一期

                                              从Java转iOS第一个项目总结 http://www.cocoachina.com/ios/20150417/11595.html icon设计探讨:图标,文字,还是图标加文字? http://ww ...

                                            7. 微信小程序中支持es7的async语法

                                              最近在原生的微信小程序项目中需要把原来es6的promise方法改成es7的async await,这样代码看起来更直观,也方便以后的兄弟维护,但是改了代码之后项目就报错了. 提示的错误是:regen ...

                                            8. LeetCode113 Path Sum II

                                              Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

                                            9. Vue.js 第3章 axios&Vue过渡动画

                                              promise 它将我们从回调地狱中解脱出来 创建和使用 var fs = require('fs') // 创建promise // reslove表示执行成功后调用的回调函数 // reject表 ...

                                            10. notepad++最有用的快捷键

                                              Ctrl+Tab  实现在多个打开的窗口间切换 Ctrl+Shift+Q 区块注释 Ctrl+K  行注释 Tab 缩进 Shift+Tab 删除缩进 先按住键盘上的“ctrl”键不放,然后滚动鼠标的 ...