BCrypt helps keep passwords safe by turning them into secret codes. This way, even if someone sees the code, they can't easily guess the original password.
Password encoding with BCrypt in Spring Boot
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Spring Boot
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encodedPassword = encoder.encode(rawPassword);
boolean matches = encoder.matches(rawPassword, encodedPassword);Use encode() to turn a plain password into a secure code.
Use matches() to check if a typed password matches the stored code.
Examples
Spring Boot
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encoded = encoder.encode("mySecret123");Spring Boot
boolean isMatch = encoder.matches("mySecret123", encoded);Spring Boot
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(12);Sample Program
This program encodes a password and then checks if the original password matches the encoded one.
Spring Boot
package com.example.demo; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; public class PasswordEncoderDemo { public static void main(String[] args) { BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String rawPassword = "helloWorld"; String encodedPassword = encoder.encode(rawPassword); System.out.println("Raw password: " + rawPassword); System.out.println("Encoded password: " + encodedPassword); boolean matches = encoder.matches(rawPassword, encodedPassword); System.out.println("Password matches: " + matches); } }
Important Notes
Each time you encode the same password, the result looks different because BCrypt adds random salt.
Never store plain passwords, always store the encoded version.
Use a strength of 10 or higher for good security without slowing your app too much.
Summary
BCrypt turns passwords into secure codes to protect user data.
Use encode() to create the code and matches() to check passwords.
Always store encoded passwords, never plain text.
Practice
1. What is the main purpose of using
BCryptPasswordEncoder in Spring Boot?easy
Solution
Step 1: Understand BCryptPasswordEncoder role
BCryptPasswordEncoder is used to convert plain passwords into a secure encoded form.Step 2: Identify correct purpose
It does not decode or generate passwords, only encodes them securely.Final Answer:
To securely encode passwords before storing them -> Option DQuick Check:
Password encoding = Secure storage [OK]
Hint: BCrypt encodes, never decodes passwords [OK]
Common Mistakes:
- Thinking BCrypt can decode passwords
- Confusing encoding with password generation
- Using it for unrelated tasks like email validation
2. Which of the following is the correct way to create a
BCryptPasswordEncoder instance in Spring Boot?easy
Solution
Step 1: Recall Java object creation syntax
In Java, to create an object, use thenewkeyword followed by the constructor.Step 2: Match correct syntax
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); correctly usesnew BCryptPasswordEncoder();to create an instance.Final Answer:
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); -> Option BQuick Check:
Object creation = new + constructor [OK]
Hint: Use 'new' keyword to create objects in Java [OK]
Common Mistakes:
- Omitting 'new' keyword when creating objects
- Calling methods instead of constructors
- Incorrect method chaining in object creation
3. Given the following code snippet, what will be the output of
matches method?BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String rawPassword = "mypassword";
String encodedPassword = encoder.encode(rawPassword);
boolean result = encoder.matches("mypassword", encodedPassword);
System.out.println(result);medium
Solution
Step 1: Understand encode and matches methods
Theencodemethod creates a hashed password. Thematchesmethod checks if the raw password matches the encoded hash.Step 2: Analyze the code flow
The raw password "mypassword" is encoded, thenmatchescompares the same raw password with the encoded one, so it returns true.Final Answer:
true -> Option AQuick Check:
matches(raw, encoded) = true if same password [OK]
Hint: matches() returns true if raw matches encoded password [OK]
Common Mistakes:
- Assuming encode returns plain text
- Thinking matches compares encoded strings directly
- Expecting false because encoded password looks different
4. Identify the error in the following Spring Boot code snippet for password encoding:
BCryptPasswordEncoder encoder;
String encoded = encoder.encode("secret");medium
Solution
Step 1: Check variable initialization
The variableencoderis declared but not assigned an instance before callingencode.Step 2: Understand consequences
Using an uninitialized object causes a NullPointerException at runtime.Final Answer:
encoder is not initialized before use -> Option CQuick Check:
Uninitialized objects cause runtime errors [OK]
Hint: Always initialize objects before calling methods [OK]
Common Mistakes:
- Forgetting to create new instance with 'new'
- Assuming declaration equals initialization
- Ignoring runtime NullPointerException
5. You want to store user passwords securely in your Spring Boot application. Which approach correctly uses
BCryptPasswordEncoder to encode and verify passwords during login?hard
Solution
Step 1: Understand secure password storage
Passwords must be encoded before storing; plain text storage is insecure.Step 2: Verify password correctly on login
Usematches(rawPassword, storedEncodedPassword)to check if input matches stored hash without decoding.Final Answer:
Encode password on registration, store encoded; on login, use matches(rawPassword, storedEncodedPassword) -> Option AQuick Check:
Use matches() to verify passwords securely [OK]
Hint: Use matches() to check raw vs encoded passwords [OK]
Common Mistakes:
- Comparing encoded strings directly (they differ each time)
- Storing plain text passwords
- Trying to decode encoded passwords (not possible)
