1. 用spring security里的加密方式
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
例子
@SpringBootTest
@RunWith(SpringRunner.class)
public class PasswordEncoderTest {
// 从配置文件中获得
private static final String secretKey = "ff4314b2a07c9604d8cb25b9f08ccd22c8e";
private static final PasswordEncoder encoder = new StandardPasswordEncoder(secretKey);
/**
* 加密
* @param rawPassword 原始密码
*/
public static String encrypt(String rawPassword) {
return encoder.encode(rawPassword);
}
/**
* 匹配密码
* @param rawPassword 原始密码
* @param password 匹配密码
*/
public static boolean match(String rawPassword, String password) {
return encoder.matches(rawPassword, password);
}
@Test
public void test() throws ParseException {
System.out.println(PasswordEncoderTest.encrypt("test"));
System.out.println(PasswordEncoderTest.encrypt("test"));
System.out.println(PasswordEncoderTest.encrypt("test"));
System.out.println(PasswordEncoderTest.match("test", PasswordEncoderTest.encrypt("test")));
System.out.println(PasswordEncoderTest.match("taUlJhIxRks", "5616b8181eea5281386522fec74a060ac765110db9be14f722b41e312dced928fe3b18e78ba676b1"));
}
}
输出
cc88d67ded09c6fda80984f6402244b8537adaad295cdbc0e649d7824b8942a8e5d8108f27b4cbfc
06973b058341190c7220560030c1ebdd76a184abde5dfad56bf188c535d48f477ab2720dd61a2c83
fe848b2db9759262288704e832a2e965320451bb8df432fa6c9165f42f38053027947f5f68d84fa2
true
true