public class PasswordTest { private BCryptPasswordEncoder passwordEncoder; @BeforeEach public void init(){ passwordEncoder = new BCryptPasswordEncoder(); }
@Test //转码 public void test(){ String encode1 = passwordEncoder.encode("1234"); String encode2 = passwordEncoder.encode("1234"); System.out.println(encode1); System.out.println(encode2); }
}
转码输出的就是加密后的字符串,如下:
进行测试,密码对比:
@Test //加密测试 public void test2(){ boolean matches1 = passwordEncoder.matches("1234","$2a$10$pcGOVDT6A0Jxc/.dHZgf9uGwoUN9nx8n.1Ilxcmcl/qlc1.tnYdue"); boolean matches2 = passwordEncoder.matches("1234","$2a$10$xlZqE2MvTSxwpQGrx9MNL.9IsjXYIo9.IK35rnGdtTtzBpbEzDqGq"); System.out.println(matches1); System.out.println(matches2); }