Why Never Store Passwords in Plaintext – Java Security
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Passwords should never be stored in plaintext because a read of the user table, backup, log, or support export would reveal them immediately. That compromise can also spread to other sites when users reuse passwords.
Instead, the service stores a verifier produced by a password-specific hashing function such as Argon2id. The encoded value includes a unique salt and work settings. At login, PasswordEncoder.matches processes the candidate with those stored settings and checks the result; the application does not decrypt anything and never needs the original password after registration.
A leaked password hash is still valuable to an attacker because guesses can be tested offline. The algorithm must therefore be deliberately expensive, its settings must be benchmarked and raised over time, and each password needs its own salt. Strong-password controls, rate limiting, MFA, monitoring, and a credential-reset plan provide additional layers.
Why Never Store Passwords in Plaintext – Java Security
Direct Answer
Passwords must not be stored in plaintext because anyone who reads the user table, backup, log, or support export immediately learns every password. Store a salted password hash produced by a password-specific function such as Argon2id; the application verifies a login by running the candidate through the same function. A leaked hash can still be guessed offline, so strong work settings, unique salts, rate limits, MFA, and breach response are still needed.
Code Example
@Service
class RegistrationService {
private final UserRepository users;
private final PasswordEncoder passwordEncoder;
void register(String email, char[] rawPassword) {
String verifier = passwordEncoder.encode(CharBuffer.wrap(rawPassword));
users.save(new User(email, verifier));
Arrays.fill(rawPassword, '\0');
}
boolean passwordMatches(User user, CharSequence candidate) {
return passwordEncoder.matches(candidate, user.passwordHash());
}
}
Store only the encoded verifier. Never log either the password or its candidate value, and do not compare a newly encoded value with equals because modern encoders generate a fresh salt.
Common Mistakes & Pitfalls
- Using encryption instead of hashing: Freshers often think AES-encrypting passwords is secure, but encryption requires storing a decryption key somewhere—in config files, environment variables, or key vaults. If an attacker compromises the application, they get both encrypted passwords and the key to decrypt them. Hashing is one-way by design.
- Logging passwords during debugging: Adding log.info("User password: " + password) during development seems harmless, but logs persist in Splunk, CloudWatch, or text files for months. One careless debug statement leaks credentials to everyone with log access.
- Storing passwords in JWT tokens or session data: Putting the raw password in a JWT claim or Redis session cache defeats the entire purpose of hashing—these storage mechanisms are less protected than your primary database.
Check your understanding
Answer these without looking back at the article.
- What is a salt and why does it prevent rainbow table attacks?
- What is the bcrypt cost factor and how do you choose it?
- What is the difference between bcrypt and Argon2?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.