Complete the code to create a BCrypt password encoder bean.
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @Configuration public class SecurityConfig { @Bean public BCryptPasswordEncoder passwordEncoder() { return new [1](); } }
The BCryptPasswordEncoder class is used to create a password encoder bean for BCrypt hashing.
Complete the code to encode a raw password using BCryptPasswordEncoder.
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String rawPassword = "mySecret123"; String encodedPassword = encoder.[1](rawPassword);
The encode method of BCryptPasswordEncoder hashes the raw password securely.
Fix the error in the code to verify a raw password against an encoded password.
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String rawPassword = "password123"; String encodedPassword = "$2a$10$7QJH1..."; boolean matches = encoder.[1](rawPassword, encodedPassword);
The matches method takes the raw password first, then the encoded password to verify if they match.
Fill both blanks to correctly verify a password using BCryptPasswordEncoder.
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); boolean isValid = encoder.[1]([2], encodedPassword);
The matches method checks if the rawPassword matches the encodedPassword.
Fill all three blanks to create a Spring service that encodes a password and verifies it.
import org.springframework.stereotype.Service; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @Service public class PasswordService { private final BCryptPasswordEncoder encoder = new [1](); public String encodePassword(String [2]) { return encoder.[3]([2]); } public boolean verifyPassword(String rawPassword, String encodedPassword) { return encoder.matches(rawPassword, encodedPassword); } }
The service uses BCryptPasswordEncoder to encode the password by calling the encode method.