springsecurity密碼加密


<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);
    }

}

 

參考:Spring Security入門(三):密碼加密


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM