SpringSecurity_BCryptPasswordEncoder类(登录加密)

使用背景:新开一个H5的项目,需要登录后暂时个人信息.这里只需要验证用户名密码是否正确,所有只是引用了BCryptPasswordEncoder类进行密码匹配.(原项目后台是用SpringSecurity进行的加密,没有盐)

<!--security-->
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-test</artifactId>
	<scope>test</scope>
</dependency>

或者

<!--Spring Security的核心是spring-security-core, 它包含认证和访问权限控制功能-->
<!--我用的是这个依赖-->
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-core</artifactId>
	<version>5.0.6.RELEASE</version>
</dependency>
// 测试比较明文明密码与数据库密码,项目中根据实际需求修改
public class passwordEncoderUtil {
    public static void main(String[] args) {
		// 加密 同一个明文加密两次,却输出了不同的结果,但是匹配匹配结果仍为true
        System.out.println(new BCryptPasswordEncoder().encode("123456"));

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode1 = bCryptPasswordEncoder.encode("123456");
        System.out.println("加密密码:" + encode1);
		// 比较明文密码与数据库的密码
        boolean matches1 = bCryptPasswordEncoder.matches("123456", "$2a$10$ZLTmLN24fg36gBe6t3NoMOW1BreOi4PfbZdy3lj5JDx01MpvkBz2a");
        System.out.println("比较密码:" + matches1);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值