如何在spring boot項目中使用Spring Security的BCryptPasswordEncoder類進(jì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項目中使用Spring Security的BCryptPasswordEncoder類進(jìn)行相同密碼不同密文的加密和驗證的文章就介紹到這了,更多相關(guān)Spring Security BCryptPasswordEncoder類加密和驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jsoup獲取全國地區(qū)數(shù)據(jù)屬性值(省市縣鎮(zhèn)村)
這篇文章主要介紹了Jsoup獲取全國地區(qū)數(shù)據(jù)屬性值(省市縣鎮(zhèn)村)的相關(guān)資料,需要的朋友可以參考下2015-10-10maven打包web項目時同時打包為war和jar文件的方法
本篇文章主要介紹了maven打包web項目時同時打包為war和jar文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10