[Error log] After encrypting with Security's BCryptPasswordEncoder, there is a problem with the logic to modify the password

This error occurred when I was working on a rights management project

error code

	 @Override
    public Results<Void> changePassword(String username, String oldPassword, String newPassword) {
    
    
        // 根据用户名查询
        SysUser user = sysUserMapper.selcetUserByUsername(username);
        // 判断用户是否为空
        if (user == null) {
    
    
            return Results.failure(1, "用户不存在");
        }

        // 校验之前的密码是否正确, 这里就是错误所在
        if (!(new BCryptPasswordEncoder().encode(user.getPassword()).equals(oldPassword))) {
    
    
            return Results.failure(1, "旧密码错误");
        }
        // 修改数据库中的密码
        sysUserMapper.update(user.getId(), new BCryptPasswordEncoder().encode(newPassword));
        return Results.success();
    }


Because after a string is encrypted by BCryptPasswordEncoder, the generated ciphertext is different, so this comparison cannot be done here, and the matching method of BCryptPasswordEncoder is required


    @Override
    public Results<Void> changePassword(String username, String oldPassword, String newPassword) {
    
    
        // 根据用户名查询
        SysUser user = sysUserMapper.selcetUserByUsername(username);
        // 判断用户是否为空
        if (user == null) {
    
    
            return Results.failure(1, "用户不存在");
        }

        // 校验之前的密码是否正确
        if (!new BCryptPasswordEncoder().matches(oldPassword, user.getPassword())) {
    
    
            return Results.failure(1, "旧密码错误");
        }
        // 修改数据库中的密码
        sysUserMapper.update(user.getId(), new BCryptPasswordEncoder().encode(newPassword));
        return Results.success();
    }


Guess you like

Origin blog.csdn.net/qq_42380734/article/details/107091291