1 Star 3 Fork 0

LELE / BCryptPasswordEncoder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

一 介绍

spring security中的BCryptPasswordEncoder方法采用SHA-256 +随机盐+密钥对密码进行加密。SHA系列是Hash算法,不是加密算法,使用加密算法意味着可以解密(这个与编码/解码一样),但是采用Hash处理,其过程是不可逆的。 (不可逆加密SHA: 基本原理:加密过程中不需要使用密钥,输入明文后由系统直接经过加密算法处理成密文,这种加密后的数据是无法被解密的,无法根据密文推算出明文。 RSA算法历史:底层-欧拉函数)

1)加密(encode):注册用户时,使用SHA-256+随机盐+密钥把用户输入的密码进行hash处理,得到密码的hash值,然后将其存入数据库中。

2)密码匹配(matches):用户登录时,密码匹配阶段并没有进行密码解密(因为密码经过Hash处理,是不可逆的),而是使用相同的算法把用户输入的密码进行hash处理,得到密码的hash值,然后将其与从数据库中查询到的密码hash值进行比较。如果两者相同,说明用户输入的密码正确。

二 案例使用

2.1 添加依赖

      <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
             <version>5.7.6</version>
        </dependency>

2.2 PasswordConfig

为了防止有人能根据密文推测出salt,我们需要在使用BCryptPasswordEncoder时配置随即密钥,创建一个PasswordConfig配置类,注册BCryptPasswordEncoder对象:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.security.SecureRandom;
@Data
@Configuration
@ConfigurationProperties(prefix = "encoder.crypt")
public class PasswordConfig {
    /**
     * 加密强度
     */
    private int strength;
    /**
     * 干扰因子
     */
    private String secret;

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        //System.out.println("secret = " + secret);
        //对干扰因子加密
        SecureRandom secureRandom = new SecureRandom(secret.getBytes());
        //对密码加密
        return new BCryptPasswordEncoder(strength, secureRandom);
    }
}

2.3 application.yml

encoder:
  crypt:
    secret: ${random.uuid} # 随机的密钥,使用uuid
    strength: 6 # 加密强度4~31,决定盐加密时的运算强度,超过10以后加密耗时会显著增加

2.4 单元测试


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@SpringBootTest
class ApplicationTests {
    final static private String password = "123456";
    @Autowired
    private BCryptPasswordEncoder encoder;

    @Test
    void savePassword() {
        // encode():对明文字符串进行加密
        //注册用户时,使用SHA-256+随机盐+密钥把用户输入的密码进行hash处理,得到密码的hash值,然后将其存入数据库中。
        String encode1 = encoder.encode(password);
        System.out.println("encode1:" + encode1);
        String encode2 = encoder.encode(password);
        System.out.println("encode2:" + encode2);

        // matches():对加密前和加密后是否匹配进行验证
        //用户登录时,密码匹配阶段并没有进行密码解密(因为密码经过Hash处理,是不可逆的),
        // 而是使用相同的算法把用户输入的密码进行hash处理,得到密码的hash值,然后将其与从数据库中查询到的密码hash值进行比较。
        // 如果两者相同,说明用户输入的密码正确。
        boolean matches1 = encoder.matches(password, encode1);
        System.out.println("matches1:" + matches1);
        boolean matches2 = encoder.matches(password, encode2);
        System.out.println("matches2:" + matches2);

    }
}

2.5 结果

在这里插入图片描述

SpringSecurity中的BCryptPasswordEncoder算法 SpringSecurity中的密码加密算法:BCryptPasswordEncoder BCryptPasswordEncoder使用

空文件

简介

BCryptPasswordEncoder的使用及原理 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/wl8888/bcrypt-password-encoder.git
git@gitee.com:wl8888/bcrypt-password-encoder.git
wl8888
bcrypt-password-encoder
BCryptPasswordEncoder
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891