Home JavaSpring boot Password Encoder Match not working Spring boot

Password Encoder Match not working Spring boot

by Vermadeals
PASSWORD ENCODER MATCH NOT WORKING SPRING BOOT

Answer –

passwordEncoder.matches(loginDTO.getEmail())

'matches(java.lang.CharSequence, java.lang.String)' in 'org.springframework.security.crypto.password.PasswordEncoder' cannot be applied to '(java.lang.String)'

Solution –

The error message you are seeing is indicating that the matches method of the PasswordEncoder interface expects two arguments: a CharSequence and a String. However, the code is passing a String as the first argument.

To fix this error, you can convert the String to a CharSequence before calling the matches method. You can do this using the CharSequence interface or by using a String as a CharSequence directly.

Here’s an example of how to fix the error using the CharSequence interface:

CharSequence password = loginDTO.getPassword();
String encodedPassword = user.getPassword(); // assume this is the encoded password from the database
if (passwordEncoder.matches(password, encodedPassword)) {
    // Passwords match
} else {
    // Passwords do not match
}

And here’s an example of how to fix the error by using a String as a CharSequence directly:

String password = loginDTO.getPassword();
String encodedPassword = user.getPassword(); // assume this is the encoded password from the database
if (passwordEncoder.matches(password, encodedPassword)) {
    // Passwords match
} else {
    // Passwords do not match
}

Either way, the important thing is to ensure that the first argument to the matches method is a CharSequence.

You may also like

Leave a Comment