早期使用md5對密碼進行編碼,每次算出的md5值都一樣,這樣非常不安全,Spring Security推薦使用 BCryptPasswordEncoder對密碼加隨機鹽,每次的Hash值都不一樣,安全性高。
使用教程:
1、在pom文件中添加所屬依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
2、加解密:
public void testPasswrodEncoder() {
//原始密碼
String password = "111111";
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
//使用BCrypt加密,每次加密使用一個隨機鹽
String encode = encoder.encode(password);
System.out.println(encode);
boolean matches = encoder.matches(password, encode);
System.out.println(matches);
}
結果:
每次計算出的Hash值都是不一樣的,第二次執行的Hash值結果: