【问题标题】:Set In Property File - Bcrypt datasource password over Spring Boot在属性文件中设置 - Spring Boot 上的 Bcrypt 数据源密码
【发布时间】:2020-07-19 19:02:27
【问题描述】:

使用 Spring Boot 2+ 我有一个包含以下内容的 application.properties 文件:

这是我application.properties的一部分:

spring.datasource.password={bcrypt}xxxxxxx

如果没有 bcrypt,应用程序可以完美运行,但是使用它,我的代码在 DB JPA 登录中返回错误。

我已将此添加到我的安全类中:

@Autowired
private DataSource dataSource;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(passwordEncoder());
}

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

但这并不能解决我的问题。 仍然无法登录数据库。

有人可以帮忙吗? 在此先感谢各位。

【问题讨论】:

  • 去掉{bcrypt},它只在你使用DelegatingPasswordEncoder时使用。请参阅此答案以了解更多stackoverflow.com/questions/62701158/…
  • 如果仍然失败,在BCryptPasswordEncoder.matches方法中下一个断点,它将比较你发送的密码和你在application.properties中设置的密码
  • 嗨,@KavithakaranKanapathippillai 感谢您的回答。我迷路了,因为您的答案与 DB 中的 Bcrypt 密码有关。我的重点是哈希我的数据库密码连接.. 为什么这么难?
  • 您的密码也来自 db。在您的配置中查看jdbcAuthentication()
  • 你有 github 仓库吗?

标签: java spring spring-boot spring-data-jpa jpa-2.0


【解决方案1】:

我能够通过使用“spring boot jasypt”api 解决这个问题。 非常感谢你的助手。 亚尼夫

这是一个教程,可以在您需要时为您提供帮助。 https://www.baeldung.com/spring-boot-jasypt

【讨论】:

    【解决方案2】:

    在你的代码中的某处必须有某种密码比较,所以如果你的密码被 bcrypt 保存和加密,你也必须用 bycrypt 检查它 与

    BCryptPasswordEncoder.matches(rawPassword, encryptedPassword);
    

    例如,在我的代码中:

    @Service
    public class UserServiceImpl implements AuthenticationProvider {
    
      @Autowired
      private final UserRepository userRepository;
      @Autowired
      private final PasswordEncoder passwordEncoder;
    
      @Override
      public Authentication authenticate(Authentication authentication) {
        String username = authentication.getName();
        String password = authentication.getCredentials().toString();
        User user = userRepository.find.findByUsername(username);
        //You have to check if user exist or not before compares the password
        if (passwordEncoder.matches(password, user.getPassword())) {
            //Password matches then login
        } else {
            //Password did not match, bad credentials.
        }
      }
    }
    

    【讨论】:

    • 您好 @kyito ,这是 SQL Server 的 DATASOURCE JDBC 登录。数据源是 Spring boot 包中没有类的默认数据源。如何设置要散列的数据源 (JDBC) 的密码?
    • 你能提供你的代码你是如何登录的吗?您的代码中的某处必须检查用户和密码是否匹配。
    猜你喜欢
    • 2020-01-27
    • 2016-04-25
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2016-05-03
    • 1970-01-01
    • 2020-08-21
    相关资源
    最近更新 更多