Spring security密碼加密實現代碼實例
xml配置如下
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-core</artifactId> <version>3.2.3.RELEASE</version></dependency>
BCryptPasswordEncoder相關知識:
用戶表的密碼通常使用MD5等不可逆算法加密后存儲,為防止彩虹表破解更會先使用一個特定的字符串(如域名)加密,然后再使用一個隨機的salt(鹽值)加密。
特定字符串是程序代碼中固定的,salt是每個密碼單獨隨機,一般給用戶表加一個字段單獨存儲,比較麻煩。
BCrypt算法將salt隨機并混入最終加密后的密碼,驗證時也無需單獨提供之前的salt,從而無需單獨處理salt問題。
代碼如下
import org.springframework.security.crypto.bcrypt.BCrypt;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;public class Test { public static void main(String[] args) { // springsecurity 注冊加密方法 BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); String encode = bCryptPasswordEncoder.encode('1'); System.out.println(encode); //$2a$10$H2HTe3SVdKMk8ewC3gRKouva7U6DAQspHqyhcdg805JGHAApV1Wci //$2a$10$Iz4Y52GmirUf5SRW6jTIA.0cgaS0mKTYZVN2cFFeK8DXk9YHVhJDW // springsecurity 登錄加密方法 BCrypt bCrypt = new BCrypt(); String hashpw = bCrypt.hashpw('1', '$2a$10$Iz4Y52GmirUf5SRW6jTIA.0cgaS0mKTYZVN2cFFeK8DXk9YHVhJDW'); System.out.println(hashpw); }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: