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

posted @ 2020-05-19 21:54  Leader_TBlog  阅读(833)  评论(0编辑  收藏  举报