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

加密:


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

判断:

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

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

举例说明:


  1. public class BCryptPasswordEncoderTest {
  2. public static void main(String[] args) {
  3. String pass = "admin";
  4. BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
  5. String hashPass = bcryptPasswordEncoder.encode(pass);
  6. System.out.println(hashPass);
  7. boolean f = bcryptPasswordEncoder.matches("admin",hashPass);
  8. System.out.println(f);
  9. }
  10. }

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

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

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

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

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


  1. //******BCrypt.java******salt即取出要比较的DB中的密码*******
  2. real_salt = salt.substring(off + 3, off + 25);
  3. try {
  4. // ***************************************************
  5. passwordb = (password + (minor >= 'a' ? "\000" : "")).getBytes("UTF-8");
  6. }
  7. catch (UnsupportedEncodingException uee) {}
  8. saltb = decode_base64(real_salt, BCRYPT_SALT_LEN);
  9. B = new BCrypt();
  10. 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)加密及判断密码是否相同的更多相关文章

  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. phpcms 允许英文目录有空格

    大家都用过phpcm添加栏目吧,在添加栏目里面,有个选项是 英文目录,这里目录可以用作伪静态功能.这么英文不能有空格等特殊字符.但是如果页面中需要引用包含空格的字符呢,例如,关于我们页面,我要显示英文 ...

  2. 如何使用Data Lake Analytics创建分区表

    前言 Data Lake Analytics(后文简称DLA)提供了无服务化的大数据分析服务,帮助用户通过标准的SQL语句直接对存储在OSS.TableStore上的数据进行查询分析. 在关系型数据库 ...

  3. 深入浅出Cocoa 之动态创建类【转】

    在前文<深入浅出Cocoa之类与对象>一文中,我已经详细介绍了ObjC中的 Class 与 Object 的概念,今天我们来如何在运行 时动态创建类.下面这个函数就是应用前面讲到的Clas ...

  4. 在APPfuse中配置log4j进行定位

    appFuse使用log4j进行日志输出,默认日志级别为warn,输出到串口console. 一下修改为debug级别,输出到串口及文件中: 1.修改(tomcat中的页面根目录)/appfuse/W ...

  5. 笔记:OSAL st 宏学习 do { x } while (__LINE__ == -1)

    笔记:OSAL st 宏学习 do { x } while (LINE == -1) #define st(x) do { x } while (__LINE__ == -1) 这段的意思是让代码可以 ...

  6. day39-Spring 14-Spring的JDBC模板:DBCP连接池配置

    一般常用的连接池是DBCP和C3P0. package cn.itcast.spring3.demo1; import java.sql.DriverManager; import org.junit ...

  7. js赋值符号“=”的小例子

    var obj1={x:5}; var obj2=obj1; obj1.a=obj1={x:6}; console.log(obj1.a); console.log(obj2.a); 为什么obj1. ...

  8. pytorch利用多个GPU并行计算多gpu

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Answer3664/article/de ...

  9. 2019-7-4-win10-uwp-处理用户点击关闭按钮

    title author date CreateTime categories win10 uwp 处理用户点击关闭按钮 lindexi 2019-07-04 09:28:57 +0800 2019- ...

  10. 【uml】之类图中的关系 标签: uml图形类 2014-11-29 09:02 1198人阅读 评论(23)

    uml早就画完了图,但是自己迟迟没有总结,因为总觉的自己把握的不到位,虽然现在也还是不到位,废话少说,上篇博客总结了用例图中的几种关系,这篇就讨论一下类图中的几种关系. 在uml的所有图中,就我目前的 ...