欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何在spring boot項目中使用Spring Security的BCryptPasswordEncoder類進行相同密碼不同密文的加密和驗證

 更新時間:2024年10月12日 11:18:49   作者:南山十一少  
本文介紹如何在Spring Boot項目中通過修改pom.xml引入安全依賴,添加配置類以解除默認的HTTP請求攔截,以及如何創(chuàng)建BCryptPasswordEncoder對象進行密碼的加密和匹配,通過這些步驟,可以有效地增強應用的安全性

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. 增加配置類設置

當引入 spring-boot-starter-security 后,Spring Security 會自動應用默認的安全配置,所有的 HTTP 請求都會被攔截并需要進行身份認證。使用下列配置類解除攔截

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. 對密碼進行密文加密

使用此方法對密碼加密,即是傳入相同的明文密碼,每次加密得到的密文結果都不一樣

encodePassWord = bCryptPasswordEncoder.encode(passWord);

6. 對密碼進行密文和明文的匹配

bCryptPasswordEncoder.matches(password, encodePassWord)

到此這篇關于在spring boot項目中使用Spring Security的BCryptPasswordEncoder類進行相同密碼不同密文的加密和驗證的文章就介紹到這了,更多相關Spring Security BCryptPasswordEncoder類加密和驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論