SpringBoot整合BCrypt实现密码加密
更新时间:2021年11月26日 17:34:16 作者:小郑要做干饭人
这篇文章主要为大家详细介绍了SpringBoot整合BCrypt进行密码加密,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了SpringBoot整合BCrypt实现密码加密的具体代码,供大家参考,具体内容如下
一. 首先在pom依赖中加入依赖:
1 2 3 4 5 6 7 8 9 | <!-- security依赖包 (加密) --> < dependency > < groupId >org.springframework.security</ groupId > < artifactId >spring-security-web</ artifactId > </ dependency > < dependency > < groupId >org.springframework.security</ groupId > < artifactId >spring-security-config</ artifactId > </ dependency > |
二.在启动类中加入@EnableScheduling注解,获得BCrypt支持:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.zzx; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.annotation.EnableScheduling; //获得BCrypt支持 @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication. class , args); } } |
三.模拟获得登录密码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.zzx.test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; /** * @date: 2021/11/25/ 14:30 * @author: ZhengZiXuan * @title: 使用BCrypt进行密码加密 * @description: 引入Security依赖默认开启了登录校验,访问API会跳转到登录页,如果只是需要BCrypt加密功能可以在启动类配置@SpringBootApplication (exclude = { org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class })禁用Security相关功能。 */ public class BCryptTest { public static void main(String[] args) { //模拟从前端获得的密码 String password = "123456" ; BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); String newPassword = bCryptPasswordEncoder.encode(password); System.out.println( "加密的密码为: " +newPassword); boolean same_password_result = bCryptPasswordEncoder.matches(password,newPassword); //返回true System.out.println( "相同代码对比: " +same_password_result); boolean other_password_result = bCryptPasswordEncoder.matches( "1234456" ,newPassword); //返回false System.out.println( "其他密码对比: " + other_password_result); } } |
运行结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
- 解决Spring security5.5.7报错Encoded password does not look like BCrypt异常
- 使用spring security BCryptPasswordEncoder接入系统
- 如何在spring boot项目中使用Spring Security的BCryptPasswordEncoder类进行相同密码不同密文的加密和验证
- 一文掌握SpringSecurity BCrypt密码加密和解密
- Springboot基于BCrypt非对称加密字符串的实现
- Spring security BCryptPasswordEncoder密码验证原理详解
- Spring项目使用Maven和BCrypt实现修改密码功能方式

微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
如何理解Java中基类子对象的构建过程从"基类向外"进行扩散的?
今天小编就为大家分享一篇关于如何理解Java中基类子对象的构建过程从"基类向外"进行扩散的?,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧2019-04-04
最新评论