【问题标题】:Spring security encoded password does not look like BCrypt while values are equals值相等时,Spring 安全编码密码看起来不像 BCrypt
【发布时间】:2017-08-28 09:08:10
【问题描述】:

我正在使用带有 BCrypt 的 Spring Security,但我在解决问题时遇到了一些麻烦。

所以我的 sn-p 我在哪里设置 passwordEncoder

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

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

在数据库中密码正确保存为 BCrypt,但是当我尝试进行身份验证时收到错误消息

Encoded password does not look like BCrypt

    "status": 401,
    "error": "Unauthorized",
    "exception": "org.springframework.security.authentication.BadCredentialsException",

【问题讨论】:

标签: spring spring-security


【解决方案1】:

我也有同样的例外。在我的例子中,我手动将密码添加到数据库中,问题出现了,因为我使用在线 BCrypt 生成器生成了密码,该生成器在哈希中使用了 $2y$ 前缀——而 Java BCryptPasswordEncoder 使用 $2a$ 一个。所以,我只是使用了另一个生成器。

【讨论】:

【解决方案2】:

在bcrypt散列算法中,每次生成一个长度为60的不同散列值,例如

$2a$10$LOqePml/koRGsk2YAIOFI.1YNKZg7EsQ5BAIuYP1nWOyYRl21dlne

一个常见的错误,“password”列(users表)的长度小于60,例如password VARCHAR(45),有些数据库会自动截断数据。因此,您总是会收到“编码密码看起来不像 BCrypt”的警告。

要解决这个问题,请确保“密码”列的长度至少为 60。

参考:https://www.mkyong.com/spring-security/spring-security-encoded-password-does-not-look-like-bcrypt/

【讨论】:

  • 列的长度为 VARCHAR(255)
  • 就我而言,我使用的是 postgresql,我的密码列是 char(128)。如果密码列是 char(128),密码长度是 64,则添加 64 个空格。这就是身份验证总是失败的原因。我将其更改为 varchar(128) 并且身份验证现在正在工作。
猜你喜欢
  • 2020-05-19
  • 2021-03-20
  • 2020-05-26
  • 2018-08-18
  • 2018-10-09
  • 2019-03-25
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
相关资源
最近更新 更多