当前位置: 首页 > 知识库问答 >
问题:

使用BcryptPasswordEncoder启动spring时,密码不匹配

呼延凌
2023-03-14
    @PostMapping("/settings/passwordupdate")
    public String PasswordUpdate(@RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword, Principal principal) {

         
        String userName = principal.getName();
        User currentUser = serviceUserDetail.findByUserName(userName);
        final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        System.out.println(newPassword + " ||||  " + passwordEncoder.encode(currentUser.getPassword()));    
        if (passwordEncoder.matches(oldPassword, passwordEncoder.encode(currentUser.getPassword()))) {
             
            System.out.print("match");
            
        } else {
            System.out.print("not match");
        }
        return "redirect:/";
    }
pass ||||  $2a$10$Y3JMpBg/3l4SHJY/X8XRS.O3vLxr64iLLoLY3r933irwsnrvCIr2q
not match---------------

共有1个答案

巫健柏
2023-03-14

不能将新密码与重新编码的旧密码匹配。您可以将参数中的旧密码与当前密码匹配。

你可以这样试试:

if (passwordEncoder.matches(oldPassword, currentUser.getPassword())) {
    String encodedNewPassword = passwordEncoder.encode(newPassword);
    // Store encoded new password..
}

另请参见bcryptpasswordencoder.matches(java.lang.charsequencerawpassword,java.lang.string encodedPassword)

 类似资料:
  • 我有一个springboot 2.0.1.mvc版本的应用程序,这是我的配置文件 但当我登录到应用程序时,我在日志文件中看到了以下消息: 我不能登录...密码是正确的。我在将我的应用程序从springboot 1更新到springboot 2后出现了这个错误

  • 我正在使用OAuth2和JPA编写spring boot REST安全API。当访问访问令牌时,我会收到警告,因为编码密码看起来不像BCrypt。当我点击邮递员http://localhost:8080/oauth/token的URL时?grant_type=password&username=user&password=user i get 我已经定义了Bean和存储库。我已经使用了secret

  • 我穿着春靴很天真。我已经实现了在注册过程中加密密码的功能,但我找不到任何关于如何加密数据库中现有密码的文章(即以字符串格式显示在数据库中的密码)。请帮我解决这个问题。

  • 当我通过spring boot和访问部署我的spring应用程序时,我必须进行身份验证,但是用户名和密码是什么,或者我如何设置它?我试图将其添加到文件中,但没有成功: 这是应用程序的出发点: 这是Tomcat依赖项: 如何在上进行身份验证?

  • 我知道保持一致的哈希通常会破坏编码的目的,但现在的情况是,关闭任何内容会使我的存储库中以前的所有用户条目都无用。有什么我能帮到的吗?

  • 每当我启动Hadoop集群时,系统都会询问密码。 我已经在.ssh文件夹中添加了密钥。 开始-dfs.sh 19/01/22 20:38:56警告util.nativeCodeLoader:无法为您的平台加载本机Hadoop库...使用内置Java类(如果适用)在[localhost]xxxx@localhost's password上启动namenode:localhost:启动namenode