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

如何在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordEncoder類進(jìn)行相同密碼不同密文的加密和驗(yàn)證

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

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)文章

最新評論