应用自定义密码加密规则(加盐版)

开始吧

1、 SpringSecurity提供的BCryptPasswordEncoder加密规则。 (加盐料理)

所以直接用就好了!配置类中注入BCryptPasswordEncoder即可,改掉之前自定义的加密那个~

    //SpringSecurity 提供的 BCryptPasswordEncoder 加密规则。 (加盐料理)
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }
      ...
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                //走账号密码验证的方法
                .userDetailsService(myUserDetailsService)
                //走给输入的密码加密和比对两个方法
                .passwordEncoder(bCryptPasswordEncoder);
    }

2、 测试即可

先加密存进数据库

    public static void main(String[] args) {
        String password = "345";
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        //加密4次
        for (int i = 0; i < 4; i++) {
            System.out.println(bCryptPasswordEncoder.encode(password));
        }
        //$2a$10$DuGfkxRfB3yKHgGlJa5XMOuBWTdN/LUL2X3PVzn/l8QyTpX./tsDe
        //$2a$10$g3dPiQpPKEtEsxZa3mVFnOC0FCCnwbOL88Sm3z2E9esDYIE6a52eG
        //$2a$10$NuviTSbDkaU/CIOEyRS20OyTAcPd16mStcLSvY3XzYwkVMRwFlrlC
        //$2a$10$ppvXuLtB0QTNAxMlmzoca.Te98CfCHRzNPVR9GQKlLe7eBWLumBF6
    }

测试成功的?不,不成功!!!

3、 错误!

org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder.matches Encoded password does not look like BCrypt

笑了!搞的我输入的密码不像你BCrypt,代码是框架处理的。我也检查了,也在main方法中测试了是true的!你给我整个不像你的密码!~~~

然后我就去找数据库的麻烦!哈哈

发现的确是因为表中密码字段的长度不够!存入的加密后的字符串太长,没有存完整。导致判断的时候不匹配!

程序中出错是好事儿!但是出错多了那就是你细不细细心的问题了~

原文地址:https://www.cnblogs.com/jinyuanya/p/13951873.html