MD5 加密-解密
别再用简单版MD5加密了,大佬们都这么是使用——>MD5盐值加密多方法详解 - 掘金
使用spring提供的工具 BCryptPasswordEncoder
导入以下依赖
<dependency><groupId>org.springframework.bootgroupId><artifactId>spring-boot-starter-securityartifact><version>2.1.6.RELEASEversion>
dependency>
@Testvoid contextLoads() throws FileNotFoundException {//new一个 BCryptPasswordEncoderBCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();//调用encode方法进行加密,传入我们需要加密的数据String encode1 = passwordEncoder.encode("123456");String encode2 = passwordEncoder.encode("123456");System.out.println(encode1);System.out.println(encode2);}
使用matches方法进行校验
@Testvoid contextLoads() throws FileNotFoundException {BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();/*** $2a$10$dGhEGYydqI9sOGdslizdmuUKliBsyHdVhON0WVppVRoa5wRHe2SEO* $2a$10$h2PlE5DbDfiClGDw2jGJm.Dp9zNVqOHPADwScJQCDbvG/lzeKOxO6*/boolean test1 = passwordEncoder.matches("123456", "$2a$10$dGhEGYydqI9sOGdslizdmuUKliBsyHdVhON0WVppVRoa5wRHe2SEO");boolean test2 = passwordEncoder.matches("123456", "$2a$10$h2PlE5DbDfiClGDw2jGJm.Dp9zNVqOHPADwScJQCDbvG/lzeKOxO6");System.out.println(test1);System.out.println(test2);}