Difficulty: Intermediate
How should passwords be stored in a Spring Boot application, and why is BCrypt used instead of plain hashing?
The first rule of storing passwords is that you never store passwords. You store a one-way transformation of them, so even if your database leaks, attackers cannot simply read them. The interesting part of this question is how good that transformation needs to be, and why MD5 or SHA-256 alone are not enough.
A hash function turns any input into a fixed-size output and cannot be reversed directly. The problem is speed. General hash functions like SHA-256 are designed to be fast, which is great for checksums and terrible for passwords, because an attacker with a GPU can try billions of guesses per second against a leaked hash. Attackers also use rainbow tables, precomputed lists of hashes for common passwords. Two protections address these problems. Salting adds a random value to each password before hashing, so identical passwords produce different hashes, and precomputed tables become useless. Work factor makes each hash deliberately slow, so guessing costs real time.
BCrypt provides both. It generates a random salt automatically, embeds it in the output string, and takes a cost factor, called strength in Spring, default 10, meaning 2 to the power 10 rounds. Raising the cost by one doubles the time. A typical BCrypt result looks like $2a$10$ followed by 22 characters of salt and 31 of hash, so the algorithm version, cost, salt, and hash all live in one string and you need no separate salt column. Two hashes of the same password differ, so you cannot compare with equals; you call encoder.matches(raw, stored), which extracts the salt and cost from the stored value and recomputes.
In Spring Security you expose a PasswordEncoder bean, usually new BCryptPasswordEncoder(). You call encode when registering or changing a password, and DaoAuthenticationProvider uses matches during login. Spring also offers DelegatingPasswordEncoder, created by PasswordEncoderFactories.createDelegatingPasswordEncoder(), which prefixes hashes with an id like {bcrypt} so you can migrate to newer algorithms such as Argon2 or SCrypt over time without breaking old users. This is a good point to mention for long-lived systems, together with upgradeEncoding, which lets you re-hash a password on successful login when the cost or algorithm changes.
Some operational advice. BCrypt truncates input at 72 bytes, so very long passphrases beyond that are cut, and some teams pre-hash. Choose a cost that takes roughly a quarter second on your production hardware. Never log passwords, never return the hash from an API, and mark password fields with @JsonIgnore or keep them out of DTOs. Enforce minimum length and check against breached password lists. Add rate limiting and account lockout for login endpoints to slow online guessing, because hashing only protects data at rest.
Also distinguish encoding, encryption, and hashing in the interview. Encoding such as Base64 is reversible and offers no security. Encryption is reversible with a key and is right for data you must read back. Hashing is one-way and right for passwords. Saying the wrong word here is a red flag, even though Spring's own interface is unfortunately named PasswordEncoder.
@Configuration
class PasswordConfig {
@Bean
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12); // cost factor 12
// or: PasswordEncoderFactories.createDelegatingPasswordEncoder() for migrations
}
}
@Service
@RequiredArgsConstructor
class AuthService {
private final UserRepository users;
private final PasswordEncoder encoder;
public void register(String email, String rawPassword) {
User u = new User();
u.setEmail(email);
u.setPasswordHash(encoder.encode(rawPassword)); // salted, slow hash
users.save(u);
}
public boolean verify(String email, String rawPassword) {
return users.findByEmail(email)
.map(u -> encoder.matches(rawPassword, u.getPasswordHash()))
.orElse(false);
}
}
Never compare with equals; matches re-derives the hash using the salt inside the stored string.
PasswordEncoder, BCrypt, Hashing, Salting