先說一句:密碼是無法解密的。大家也不要再問松哥微人事項目中的密碼怎么解密了!
密碼無法解密,還是為了確保系統(tǒng)安全。今天松哥就來和大家聊一聊,密碼要如何處理,才能在最大程度上確保我們的系統(tǒng)安全。
1.為什么要加密
2011 年 12 月 21 日,有人在網(wǎng)絡(luò)上公開了一個包含 600 萬個 CSDN 用戶資料的數(shù)據(jù)庫,數(shù)據(jù)全部為明文儲存,包含用戶名、密碼以及注冊郵箱。事件發(fā)生后 CSDN 在微博、官方網(wǎng)站等渠道發(fā)出了聲明,解釋說此數(shù)據(jù)庫系 2009 年備份所用,因不明原因泄露,已經(jīng)向警方報案,后又在官網(wǎng)發(fā)出了公開道歉信。在接下來的十多天里,金山、網(wǎng)易、京東、當當、新浪等多家公司被卷入到這次事件中。整個事件中最觸目驚心的莫過于 CSDN 把用戶密碼明文存儲,由于很多用戶是多個網(wǎng)站共用一個密碼,因此一個網(wǎng)站密碼泄露就會造成很大的安全隱患。由于有了這么多前車之鑒,我們現(xiàn)在做系統(tǒng)時,密碼都要加密處理。
這次泄密,也留下了一些有趣的事情,特別是對于廣大程序員設(shè)置密碼這一項。人們從 CSDN 泄密的文件中,發(fā)現(xiàn)了一些好玩的密碼,例如如下這些:
- ppnn13%dkstFeb.1st 這段密碼的中文解析是:娉娉裊裊十三余,豆蔻梢頭二月初。
- csbt34.ydhl12s 這段密碼的中文解析是:池上碧苔三四點,葉底黃鸝一兩聲
- ...
等等不一而足,你會發(fā)現(xiàn)很多程序員的人文素養(yǎng)還是非常高的,讓人嘖嘖稱奇。
2.加密方案
密碼加密我們一般會用到散列函數(shù),又稱散列算法、哈希函數(shù),這是一種從任何數(shù)據(jù)中創(chuàng)建數(shù)字“指紋”的方法。
散列函數(shù)把消息或數(shù)據(jù)壓縮成摘要,使得數(shù)據(jù)量變小,將數(shù)據(jù)的格式固定下來,然后將數(shù)據(jù)打亂混合,重新創(chuàng)建一個散列值。散列值通常用一個短的隨機字母和數(shù)字組成的字符串來代表。好的散列函數(shù)在輸入域中很少出現(xiàn)散列沖突。在散列表和數(shù)據(jù)處理中,不抑制沖突來區(qū)別數(shù)據(jù),會使得數(shù)據(jù)庫記錄更難找到。
我們常用的散列函數(shù)有 MD5 消息摘要算法、安全散列算法(Secure Hash Algorithm)。
但是僅僅使用散列函數(shù)還不夠,單純的只使用散列函數(shù),如果兩個用戶密碼明文相同,生成的密文也會相同,這樣就增加的密碼泄漏的風險。
為了增加密碼的安全性,一般在密碼加密過程中還需要加鹽,所謂的鹽可以是一個隨機數(shù)也可以是用戶名,加鹽之后,即使密碼明文相同的用戶生成的密碼密文也不相同,這可以極大的提高密碼的安全性。
傳統(tǒng)的加鹽方式需要在數(shù)據(jù)庫中有專門的字段來記錄鹽值,這個字段可能是用戶名字段(因為用戶名唯一),也可能是一個專門記錄鹽值的字段,這樣的配置比較繁瑣。
Spring Security 提供了多種密碼加密方案,官方推薦使用 BCryptPasswordEncoder,BCryptPasswordEncoder 使用 BCrypt 強哈希函數(shù),開發(fā)者在使用時可以選擇提供 strength 和 SecureRandom 實例。strength 越大,密鑰的迭代次數(shù)越多,密鑰迭代次數(shù)為 2^strength。strength 取值在 4~31 之間,默認為 10。
不同于 Shiro 中需要自己處理密碼加鹽,在 Spring Security 中,BCryptPasswordEncoder 就自帶了鹽,處理起來非常方便。
3.實踐
3.1 codec 加密
commons-codec 是一個 Apache 上的開源項目,用它可以方便的實現(xiàn)密碼加密。松哥在 V 部落 項目中就是采用的這種方案(https://github.com/lenve/VBlog)。在 Spring Security 還未推出 BCryptPasswordEncoder 的時候,commons-codec 還是一個比較常見的解決方案。
所以,這里我先來給大家介紹下 commons-codec 的用法。
首先我們需要引入 commons-codec 的依賴:
1
2
3
4
5
|
< dependency > < groupId >commons-codec</ groupId > < artifactId >commons-codec</ artifactId > < version >1.11</ version > </ dependency > |
然后自定義一個 PasswordEncoder:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Component public class MyPasswordEncoder implements PasswordEncoder { @Override public String encode(CharSequence rawPassword) { return DigestUtils.md5DigestAsHex(rawPassword.toString().getBytes()); } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { return encodedPassword.equals(DigestUtils.md5DigestAsHex(rawPassword.toString().getBytes())); } } |
在 Spring Security 中,PasswordEncoder 專門用來處理密碼的加密與比對工作,我們自定義 MyPasswordEncoder 并實現(xiàn) PasswordEncoder 接口,還需要實現(xiàn)該接口中的兩個方法:
- encode 方法表示對密碼進行加密,參數(shù) rawPassword 就是你傳入的明文密碼,返回的則是加密之后的密文,這里的加密方案采用了 MD5。
- matches 方法表示對密碼進行比對,參數(shù) rawPassword 相當于是用戶登錄時傳入的密碼,encodedPassword 則相當于是加密后的密碼(從數(shù)據(jù)庫中查詢而來)。
最后記得將 MyPasswordEncoder 通過 @Component 注解標記為 Spring 容器中的一個組件。
這樣用戶在登錄時,就會自動調(diào)用 matches 方法進行密碼比對。
當然,使用了 MyPasswordEncoder 之后,在用戶注冊時,就需要將密碼加密之后存入數(shù)據(jù)庫中,方式如下:
1
2
3
4
5
6
7
|
public int reg(User user) { ... //插入用戶,插入之前先對密碼進行加密 user.setPassword(passwordEncoder.encode(user.getPassword())); result = userMapper.reg(user); ... } |
其實很簡單,就是調(diào)用 encode 方法對密碼進行加密。完整代碼大家可以參考 V 部落(https://github.com/lenve/VBlog),我這里就不贅述了。
3.2 BCryptPasswordEncoder 加密
但是自己定義 PasswordEncoder 還是有些麻煩,特別是處理密碼加鹽問題的時候。
所以在 Spring Security 中提供了 BCryptPasswordEncoder,使得密碼加密加鹽變得非常容易。只需要提供 BCryptPasswordEncoder 這個 Bean 的實例即可,微人事就是采用了這種方案(https://github.com/lenve/vhr),如下:
1
2
3
4
|
@Bean PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder( 10 ); } |
創(chuàng)建 BCryptPasswordEncoder 時傳入的參數(shù) 10 就是 strength,即密鑰的迭代次數(shù)(也可以不配置,默認為 10)。同時,配置的內(nèi)存用戶的密碼也不再是 123 了,如下:
1
2
3
4
5
6
7
8
|
auth.inMemoryAuthentication() .withUser( "admin" ) .password( "$2a$10$RMuFXGQ5AtH4wOvkUqyvuecpqUSeoxZYqilXzbz50dceRsga.WYiq" ) .roles( "ADMIN" , "USER" ) .and() .withUser( "sang" ) .password( "$2a$10$eUHbAOMq4bpxTvOVz33LIehLe3fu6NwqC9tdOcxJXEhyZ4simqXTC" ) .roles( "USER" ); |
這里的密碼就是使用 BCryptPasswordEncoder 加密后的密碼,雖然 admin 和 sang 加密后的密碼不一樣,但是明文都是 123。配置完成后,使用 admin/123 或者 sang/123 就可以實現(xiàn)登錄。
本案例使用了配置在內(nèi)存中的用戶,一般情況下,用戶信息是存儲在數(shù)據(jù)庫中的,因此需要在用戶注冊時對密碼進行加密處理,如下:
1
2
3
4
5
6
7
8
|
@Service public class RegService { public int reg(String username, String password) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder( 10 ); String encodePasswod = encoder.encode(password); return saveToDb(username, encodePasswod); } } |
用戶將密碼從前端傳來之后,通過調(diào)用 BCryptPasswordEncoder 實例中的 encode 方法對密碼進行加密處理,加密完成后將密文存入數(shù)據(jù)庫。
4.源碼淺析
最后我們再來稍微看一下 PasswordEncoder。
PasswordEncoder 是一個接口,里邊只有三個方法:
1
2
3
4
5
6
7
|
public interface PasswordEncoder { String encode(CharSequence rawPassword); boolean matches(CharSequence rawPassword, String encodedPassword); default boolean upgradeEncoding(String encodedPassword) { return false ; } } |
- encode 方法用來對密碼進行加密。
- matches 方法用來對密碼進行比對。
- upgradeEncoding 表示是否需要對密碼進行再次加密以使得密碼更加安全,默認為 false。
Spring Security 為 PasswordEncoder 提供了很多實現(xiàn):
但是老實說,自從有了 BCryptPasswordEncoder,我們很少關(guān)注其他實現(xiàn)類了。
PasswordEncoder 中的 encode 方法,是我們在用戶注冊的時候手動調(diào)用。
matches 方法,則是由系統(tǒng)調(diào)用,默認是在 DaoAuthenticationProvider#additionalAuthenticationChecks 方法中調(diào)用的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException { if (authentication.getCredentials() == null ) { logger.debug( "Authentication failed: no credentials provided" ); throw new BadCredentialsException(messages.getMessage( "AbstractUserDetailsAuthenticationProvider.badCredentials" , "Bad credentials" )); } String presentedPassword = authentication.getCredentials().toString(); if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) { logger.debug( "Authentication failed: password does not match stored value" ); throw new BadCredentialsException(messages.getMessage( "AbstractUserDetailsAuthenticationProvider.badCredentials" , "Bad credentials" )); } } |
可以看到,密碼比對就是通過 passwordEncoder.matches 方法來進行的。
關(guān)于 DaoAuthenticationProvider 的調(diào)用流程,大家可以參考 SpringSecurity 自定義認證邏輯的兩種方式(高級玩法)一文。
好了,今天就和小伙伴們簡單聊一聊 Spring Security 加密問題,小伙伴們要是有收獲記得點個在看鼓勵下松哥哦~
以上就是Spring Boot 中密碼加密的兩種方法的詳細內(nèi)容,更多關(guān)于Spring Boot 密碼加密的資料請關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://mp.weixin.qq.com/s/jBoU5j4YChDVwyX22SbtgA