深入淺析Spring Security5中默認(rèn)密碼編碼器
1.概述
在Spring Security 4中,可以使用內(nèi)存中身份驗(yàn)證以純文本格式存儲(chǔ)密碼。
對(duì)版本5中的密碼管理過(guò)程進(jìn)行了重大改進(jìn),為密碼編碼和解碼引入了更安全的默認(rèn)機(jī)制。這意味著如果您的Spring應(yīng)用程序以純文本格式存儲(chǔ)密碼,升級(jí)到Spring Security 5可能會(huì)導(dǎo)致問(wèn)題。
在這個(gè)簡(jiǎn)短的教程中,我們將描述其中一個(gè)潛在的問(wèn)題,并展示該問(wèn)題的解決方案。
2. Spring Security 4
我們首先展示一個(gè)標(biāo)準(zhǔn)的安全配置,它提供簡(jiǎn)單的內(nèi)存中身份驗(yàn)證(適用于Spring 4):
@Configuration public class InMemoryAuthWebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("spring") .password("secret") .roles("USER"); } @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/private/**") .authenticated() .antMatchers("/public/**") .permitAll() .and() .httpBasic(); } }
此配置定義所有/私有/映射方法的身份驗(yàn)證以及/ public /下所有內(nèi)容的公共訪問(wèn)。
如果我們?cè)赟pring Security 5下使用相同的配置,我們會(huì)收到以下錯(cuò)誤:
java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
該錯(cuò)誤告訴我們由于沒(méi)有為我們的內(nèi)存中身份驗(yàn)證配置密碼編碼器,因此無(wú)法解碼給定的密碼。
3. Spring Security 5
我們可以通過(guò)使用PasswordEncoderFactories類定義DelegatingPasswordEncoder來(lái)解決此錯(cuò)誤。
我們使用此編碼器通過(guò)AuthenticationManagerBuilder配置我們的用戶:
@Configuration public class InMemoryAuthWebSecurityConfigurer extends WebSecurityConfigurerAdapter { @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder(); auth.inMemoryAuthentication() .withUser("spring") .password(encoder.encode("secret")) .roles("USER"); } }
現(xiàn)在,通過(guò)這種配置,我們使用BCrypt以以下格式存儲(chǔ)我們的內(nèi)存中密碼:
{bcrypt}$2a$10$MF7hYnWLeLT66gNccBgxaONZHbrSMjlUofkp50sSpBw2PJjUqU.zS
雖然我們可以定義自己的一組密碼編碼器,但建議堅(jiān)持使用PasswordEncoderFactories中提供的默認(rèn)編碼器。
3.1.遷移現(xiàn)有密碼
我們可以通過(guò)以下方式將現(xiàn)有密碼更新為推薦的Spring Security 5標(biāo)準(zhǔn):
更新純文本存儲(chǔ)密碼及其編碼值:
String encoded = new BCryptPasswordEncoder().encode(plainTextPassword);
前綴散列存儲(chǔ)的密碼及其已知的編碼器標(biāo)識(shí)符:
{bcrypt}$2a$10$MF7hYnWLeLT66gNccBgxaONZHbrSMjlUofkp50sSpBw2PJjUqU.zS {sha256}97cde38028ad898ebc02e690819fa220e88c62e0699403e94fff291cfffaf8410849f27605abcbc0
當(dāng)存儲(chǔ)密碼的編碼機(jī)制未知時(shí),請(qǐng)求用戶更新其密碼
4.結(jié)論
在這個(gè)快速示例中,我們使用新的密碼存儲(chǔ)機(jī)制將有效的Spring 4內(nèi)存中認(rèn)證配置更新到Spring 5。
與往常一樣,您可以在GitHub項(xiàng)目中找到源代碼。
總結(jié)
以上所述是小編給大家介紹的Spring Security 5中默認(rèn)密碼編碼器,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Spring security密碼加密實(shí)現(xiàn)代碼實(shí)例
- Spring security基于數(shù)據(jù)庫(kù)中賬戶密碼認(rèn)證
- Spring security BCryptPasswordEncoder密碼驗(yàn)證原理詳解
- Spring Security使用數(shù)據(jù)庫(kù)認(rèn)證及用戶密碼加密和解密功能
- spring security 5.x實(shí)現(xiàn)兼容多種密碼的加密方式
- Java中SpringSecurity密碼錯(cuò)誤5次鎖定用戶的實(shí)現(xiàn)方法
- Spring Security保護(hù)用戶密碼常用方法詳解
相關(guān)文章
Java cglib為實(shí)體類(javabean)動(dòng)態(tài)添加屬性方式
這篇文章主要介紹了Java cglib為實(shí)體類(javabean)動(dòng)態(tài)添加屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02Java后端WebSocket的Tomcat實(shí)現(xiàn)
這篇文章主要介紹了Java后端WebSocket的Tomcat實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06Spring MVC @GetMapping和@PostMapping注解的使用方式
這篇文章主要介紹了Spring MVC @GetMapping和@PostMapping注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05解決idea中svn提交時(shí)performing vcs refresh時(shí)間很長(zhǎng)的問(wèn)題
這篇文章主要介紹了解決idea中svn提交時(shí)performing vcs refresh時(shí)間很長(zhǎng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09