Spring security中的BCryptPasswordEncoder

在SpringBoot1.5之前使用的是MD5進行加密處理,但是在SpringBoot2.0之後廢棄了該加密算法,採用了BCryptPasswordEncoder

BCryptPasswordEncoder中連個常用的方法

加密

BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
encode.encode(password);

比較(第一個是用戶數據的密碼,第二個是數據庫中存的密碼)

matches(CharSequence rawPassword, String encodedPassword) 

測試代碼

package com.tedu;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class BCryptPasswordEncoderTest {
	public static void main(String[] args) {
		String password = "admin";//模擬用戶輸入的密碼
		BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
		String hash = bCryptPasswordEncoder.encode(password);
		System.out.println("hash:  "+hash);
		
		boolean flag = bCryptPasswordEncoder.matches(password, hash);
		System.out.println(flag);
	}
}

控制檯輸出如下

hash:  $2a$10$8BEq9cZVl9AOmgy.zEbTwucjq1vL26DtWlZoP5ghVMHWzOt2s1g0i
true
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章