0

Using Spring boot 2+ I have an application.properties file with the followings:

This is a part of my application.properties:

spring.datasource.password={bcrypt}xxxxxxx

Without bcrypt the application works perfectly but using this, my code return error in DB JPA login.

I have added this to my security class:

@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();
}

But it doesn’t solve my issue. still login failure to DB.

Can anyone help? Thanks in advance guys.

Valijon
  • 12,667
  • 4
  • 34
  • 67
Yaniv Levy
  • 78
  • 10

2 Answers2

1

I was able to solve this quest by using "spring boot jasypt" api. Thank you very much for your assistant. Yaniv

Here is a tutorial that can assist you in case you need. https://www.baeldung.com/spring-boot-jasypt

Yaniv Levy
  • 78
  • 10
0

In somewhere in your code must have some kind of password comparing, so if your password is saved and encrypted by bcrypt, you must check it with bycrypt aswell with

BCryptPasswordEncoder.matches(rawPassword, encryptedPassword);

for example, in my code:

@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
  • 76
  • 2
  • Hello @kyito , This is a DATASOURCE JDBC Login For SQL Server. the Datasource is a default datasource withouth a class in the Spring boot pacakge. How can i Setup the Password of the datasource (JDBC) to be Hashed? – Yaniv Levy Jul 21 '20 at 06:07
  • Can you provide your code how did you do the login? Somewhere in you code has to check if user and password matches. – Kyito Jul 21 '20 at 21:43