07-Security密碼加密


Security密碼加密

密碼加密

在單元測試中 使用循環生成 10次不同的 密文,在Security中 有一個 BCryptPasswordEncoder 密碼加密的工具

@Test
void contextLoads() {
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
    for (int i = 0; i < 10; i++) {
        System.out.println(encoder.encode("123"));
    }
}

在這里可以看到 密碼都是123 但是加密過后密文都是不一樣的,如果你使用過Shiro,你就可以體驗到好處,就不用去維護數據庫表中的鹽字段了

改寫配置類

在上面控制台輸出的密文中隨便復制兩個出來

放在 AuthenticationManagerBuilder config中

@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // 基於內存的認證
    auth.inMemoryAuthentication()
            // 配置第一個
            .withUser("javaboy").password("$2a$10$LMjHXQ05Pprqyr8hPoIo5uWQUKFlwPUO2WIEKmak/oBmu8Pp/YWlm").roles("admin")
            .and()
            // 配置第二個
            .withUser("xhh").password("$2a$10$OeOh7UuDyxNpjzRg832VmuTrPemkUE.kMonN.nZTenfCejBaRLXxe").roles("user");// 到了這里,就相當於內存里面配置了兩個用戶
}

修改加密編碼器

@Bean
PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}

PostMan測試

和之前一樣測試即可。

本章小結

主要對Security中的密碼加密做了一系列的介紹,好了這就是Security加密的問題。

源碼下載地址:https://github.com/XiaoHuiHuiT/SpringSecurity-case/releases/tag/7.0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM