Spring Boot 中密碼加密的兩種方法
先說一句:密碼是無法解密的。大家也不要再問松哥微人事項(xiàng)目中的密碼怎么解密了!
密碼無法解密,還是為了確保系統(tǒng)安全。今天松哥就來和大家聊一聊,密碼要如何處理,才能在最大程度上確保我們的系統(tǒng)安全。
1.為什么要加密
2011 年 12 月 21 日,有人在網(wǎng)絡(luò)上公開了一個(gè)包含 600 萬個(gè) CSDN 用戶資料的數(shù)據(jù)庫,數(shù)據(jù)全部為明文儲(chǔ)存,包含用戶名、密碼以及注冊(cè)郵箱。事件發(fā)生后 CSDN 在微博、官方網(wǎng)站等渠道發(fā)出了聲明,解釋說此數(shù)據(jù)庫系 2009 年備份所用,因不明原因泄露,已經(jīng)向警方報(bào)案,后又在官網(wǎng)發(fā)出了公開道歉信。在接下來的十多天里,金山、網(wǎng)易、京東、當(dāng)當(dāng)、新浪等多家公司被卷入到這次事件中。整個(gè)事件中最觸目驚心的莫過于 CSDN 把用戶密碼明文存儲(chǔ),由于很多用戶是多個(gè)網(wǎng)站共用一個(gè)密碼,因此一個(gè)網(wǎng)站密碼泄露就會(huì)造成很大的安全隱患。由于有了這么多前車之鑒,我們現(xiàn)在做系統(tǒng)時(shí),密碼都要加密處理。
這次泄密,也留下了一些有趣的事情,特別是對(duì)于廣大程序員設(shè)置密碼這一項(xiàng)。人們從 CSDN 泄密的文件中,發(fā)現(xiàn)了一些好玩的密碼,例如如下這些:
ppnn13%dkstFeb.1st 這段密碼的中文解析是:娉娉裊裊十三余,豆蔻梢頭二月初。 csbt34.ydhl12s 這段密碼的中文解析是:池上碧苔三四點(diǎn),葉底黃鸝一兩聲 ...等等不一而足,你會(huì)發(fā)現(xiàn)很多程序員的人文素養(yǎng)還是非常高的,讓人嘖嘖稱奇。
2.加密方案
密碼加密我們一般會(huì)用到散列函數(shù),又稱散列算法、哈希函數(shù),這是一種從任何數(shù)據(jù)中創(chuàng)建數(shù)字“指紋”的方法。
散列函數(shù)把消息或數(shù)據(jù)壓縮成摘要,使得數(shù)據(jù)量變小,將數(shù)據(jù)的格式固定下來,然后將數(shù)據(jù)打亂混合,重新創(chuàng)建一個(gè)散列值。散列值通常用一個(gè)短的隨機(jī)字母和數(shù)字組成的字符串來代表。好的散列函數(shù)在輸入域中很少出現(xiàn)散列沖突。在散列表和數(shù)據(jù)處理中,不抑制沖突來區(qū)別數(shù)據(jù),會(huì)使得數(shù)據(jù)庫記錄更難找到。
我們常用的散列函數(shù)有 MD5 消息摘要算法、安全散列算法(Secure Hash Algorithm)。
但是僅僅使用散列函數(shù)還不夠,單純的只使用散列函數(shù),如果兩個(gè)用戶密碼明文相同,生成的密文也會(huì)相同,這樣就增加的密碼泄漏的風(fēng)險(xiǎn)。
為了增加密碼的安全性,一般在密碼加密過程中還需要加鹽,所謂的鹽可以是一個(gè)隨機(jī)數(shù)也可以是用戶名,加鹽之后,即使密碼明文相同的用戶生成的密碼密文也不相同,這可以極大的提高密碼的安全性。
傳統(tǒng)的加鹽方式需要在數(shù)據(jù)庫中有專門的字段來記錄鹽值,這個(gè)字段可能是用戶名字段(因?yàn)橛脩裘ㄒ唬部赡苁且粋€(gè)專門記錄鹽值的字段,這樣的配置比較繁瑣。
Spring Security 提供了多種密碼加密方案,官方推薦使用 BCryptPasswordEncoder,BCryptPasswordEncoder 使用 BCrypt 強(qiáng)哈希函數(shù),開發(fā)者在使用時(shí)可以選擇提供 strength 和 SecureRandom 實(shí)例。strength 越大,密鑰的迭代次數(shù)越多,密鑰迭代次數(shù)為 2^strength。strength 取值在 4~31 之間,默認(rèn)為 10。
不同于 Shiro 中需要自己處理密碼加鹽,在 Spring Security 中,BCryptPasswordEncoder 就自帶了鹽,處理起來非常方便。
3.實(shí)踐
3.1 codec 加密
commons-codec 是一個(gè) Apache 上的開源項(xiàng)目,用它可以方便的實(shí)現(xiàn)密碼加密。松哥在 V 部落 項(xiàng)目中就是采用的這種方案(https://github.com/lenve/VBlog)。在 Spring Security 還未推出 BCryptPasswordEncoder 的時(shí)候,commons-codec 還是一個(gè)比較常見的解決方案。
所以,這里我先來給大家介紹下 commons-codec 的用法。
首先我們需要引入 commons-codec 的依賴:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version></dependency>
然后自定義一個(gè) PasswordEncoder:
@Componentpublic 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 專門用來處理密碼的加密與比對(duì)工作,我們自定義 MyPasswordEncoder 并實(shí)現(xiàn) PasswordEncoder 接口,還需要實(shí)現(xiàn)該接口中的兩個(gè)方法:
encode 方法表示對(duì)密碼進(jìn)行加密,參數(shù) rawPassword 就是你傳入的明文密碼,返回的則是加密之后的密文,這里的加密方案采用了 MD5。 matches 方法表示對(duì)密碼進(jìn)行比對(duì),參數(shù) rawPassword 相當(dāng)于是用戶登錄時(shí)傳入的密碼,encodedPassword 則相當(dāng)于是加密后的密碼(從數(shù)據(jù)庫中查詢而來)。最后記得將 MyPasswordEncoder 通過 @Component 注解標(biāo)記為 Spring 容器中的一個(gè)組件。
這樣用戶在登錄時(shí),就會(huì)自動(dòng)調(diào)用 matches 方法進(jìn)行密碼比對(duì)。
當(dāng)然,使用了 MyPasswordEncoder 之后,在用戶注冊(cè)時(shí),就需要將密碼加密之后存入數(shù)據(jù)庫中,方式如下:
public int reg(User user) { ... //插入用戶,插入之前先對(duì)密碼進(jìn)行加密 user.setPassword(passwordEncoder.encode(user.getPassword())); result = userMapper.reg(user); ...}
其實(shí)很簡(jiǎn)單,就是調(diào)用 encode 方法對(duì)密碼進(jìn)行加密。完整代碼大家可以參考 V 部落(https://github.com/lenve/VBlog),我這里就不贅述了。
3.2 BCryptPasswordEncoder 加密
但是自己定義 PasswordEncoder 還是有些麻煩,特別是處理密碼加鹽問題的時(shí)候。
所以在 Spring Security 中提供了 BCryptPasswordEncoder,使得密碼加密加鹽變得非常容易。只需要提供 BCryptPasswordEncoder 這個(gè) Bean 的實(shí)例即可,微人事就是采用了這種方案(https://github.com/lenve/vhr),如下:
@BeanPasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(10);}
創(chuàng)建 BCryptPasswordEncoder 時(shí)傳入的參數(shù) 10 就是 strength,即密鑰的迭代次數(shù)(也可以不配置,默認(rèn)為 10)。同時(shí),配置的內(nèi)存用戶的密碼也不再是 123 了,如下:
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 就可以實(shí)現(xiàn)登錄。
本案例使用了配置在內(nèi)存中的用戶,一般情況下,用戶信息是存儲(chǔ)在數(shù)據(jù)庫中的,因此需要在用戶注冊(cè)時(shí)對(duì)密碼進(jìn)行加密處理,如下:
@Servicepublic 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 實(shí)例中的 encode 方法對(duì)密碼進(jìn)行加密處理,加密完成后將密文存入數(shù)據(jù)庫。
4.源碼淺析
最后我們?cè)賮砩晕⒖匆幌?PasswordEncoder。
PasswordEncoder 是一個(gè)接口,里邊只有三個(gè)方法:
public interface PasswordEncoder { String encode(CharSequence rawPassword); boolean matches(CharSequence rawPassword, String encodedPassword); default boolean upgradeEncoding(String encodedPassword) { return false; }} encode 方法用來對(duì)密碼進(jìn)行加密。 matches 方法用來對(duì)密碼進(jìn)行比對(duì)。 upgradeEncoding 表示是否需要對(duì)密碼進(jìn)行再次加密以使得密碼更加安全,默認(rèn)為 false。
Spring Security 為 PasswordEncoder 提供了很多實(shí)現(xiàn):
但是老實(shí)說,自從有了 BCryptPasswordEncoder,我們很少關(guān)注其他實(shí)現(xiàn)類了。
PasswordEncoder 中的 encode 方法,是我們?cè)谟脩糇?cè)的時(shí)候手動(dòng)調(diào)用。
matches 方法,則是由系統(tǒng)調(diào)用,默認(rèn)是在 DaoAuthenticationProvider#additionalAuthenticationChecks 方法中調(diào)用的。
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')); }}
可以看到,密碼比對(duì)就是通過 passwordEncoder.matches 方法來進(jìn)行的。
關(guān)于 DaoAuthenticationProvider 的調(diào)用流程,大家可以參考 SpringSecurity 自定義認(rèn)證邏輯的兩種方式(高級(jí)玩法)一文。
好了,今天就和小伙伴們簡(jiǎn)單聊一聊 Spring Security 加密問題,小伙伴們要是有收獲記得點(diǎn)個(gè)在看鼓勵(lì)下松哥哦~
以上就是Spring Boot 中密碼加密的兩種方法的詳細(xì)內(nèi)容,更多關(guān)于Spring Boot 密碼加密的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟2. 基于javascript處理二進(jìn)制圖片流過程詳解3. ajax請(qǐng)求添加自定義header參數(shù)代碼4. Java Lock接口實(shí)現(xiàn)原理及實(shí)例解析5. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法6. axios和ajax的區(qū)別點(diǎn)總結(jié)7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式9. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問題……10. 利用CSS3新特性創(chuàng)建透明邊框三角
