如何在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordEncoder類進(jìn)行相同密碼不同密文的加密和驗(yàn)證
1. 在maven配置文件pom.xml中引入依賴包
<!--加密模塊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2. 在啟動類MainApplication中加入bean
@Bean public BCryptPasswordEncoder getBcryptPasswordEncoder(){ return new BCryptPasswordEncoder(); }
3. 增加配置類設(shè)置
當(dāng)引入 spring-boot-starter-security
后,Spring Security 會自動應(yīng)用默認(rèn)的安全配置,所有的 HTTP 請求都會被攔截并需要進(jìn)行身份認(rèn)證。使用下列配置類解除攔截
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .anyRequest().permitAll(); return http.build(); } }
4. 創(chuàng)建BCryptPasswordEncoder對象
private BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
5. 對密碼進(jìn)行密文加密
使用此方法對密碼加密,即是傳入相同的明文密碼,每次加密得到的密文結(jié)果都不一樣
encodePassWord = bCryptPasswordEncoder.encode(passWord);
6. 對密碼進(jìn)行密文和明文的匹配
bCryptPasswordEncoder.matches(password, encodePassWord)
到此這篇關(guān)于在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordEncoder類進(jìn)行相同密碼不同密文的加密和驗(yàn)證的文章就介紹到這了,更多相關(guān)Spring Security BCryptPasswordEncoder類加密和驗(yàn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jsoup獲取全國地區(qū)數(shù)據(jù)屬性值(省市縣鎮(zhèn)村)
這篇文章主要介紹了Jsoup獲取全國地區(qū)數(shù)據(jù)屬性值(省市縣鎮(zhèn)村)的相關(guān)資料,需要的朋友可以參考下2015-10-10Java正則驗(yàn)證正整數(shù)的方法分析【測試可用】
這篇文章主要介紹了Java正則驗(yàn)證正整數(shù)的方法,結(jié)合實(shí)例形式對比分析了java針對正整數(shù)的驗(yàn)證方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08maven打包web項(xiàng)目時(shí)同時(shí)打包為war和jar文件的方法
本篇文章主要介紹了maven打包web項(xiàng)目時(shí)同時(shí)打包為war和jar文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10spring data jpa如何只查詢實(shí)體部分字段
這篇文章主要介紹了spring data jpa如何只查詢實(shí)體部分字段的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06關(guān)于Kafka消息隊(duì)列原理的總結(jié)
這篇文章主要介紹了關(guān)于Kafka消息隊(duì)列原理的總結(jié),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05