Spring Boot How to specify the PasswordEncoder?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Spring Boot, a module of the larger Spring Framework, provides a comprehensive suite designed to streamline the process of building and running Java web applications. As security is a fundamental aspect of any modern application, Spring Boot offers robust tools for encoding passwords to protect and secure user data.
What is a PasswordEncoder?
PasswordEncoder is an interface provided by Spring Security to define how passwords should be encoded (or hashed) before storage. Encoding passwords is crucial for ensuring that even if an attacker gains access to your database, they won't easily retrieve the user passwords.
Specifying the PasswordEncoder in Spring Boot
1. Common Implementations of PasswordEncoder
Spring Security provides several implementations of the PasswordEncoder interface:
- NoOpPasswordEncoder: Does not perform any encoding.
- BCryptPasswordEncoder: Uses the BCrypt hashing function.
- SCryptPasswordEncoder: Utilizes the SCrypt hashing function designed for increased computational cost.
- Pbkdf2PasswordEncoder: Leverages the PBKDF2 hashing technique.
- Argon2PasswordEncoder: Implements the Argon2 hashing function recommended by different security organizations.
2. Configuring PasswordEncoder
In a typical Spring Boot application, you define a bean for your chosen PasswordEncoder in a configuration class, which is usually annotated with @Configuration.
Example: Configuring BCryptPasswordEncoder
3. Using the PasswordEncoder
Once you have specified a PasswordEncoder, you can use it to encode passwords before saving them to the database and to verify input passwords during login:
Advanced Configuration: Using DelegatingPasswordEncoder
If your application migrates between different encoding formats over time, DelegatingPasswordEncoder becomes useful. It allows switching password encoding strategies seamlessly.
The DelegatingPasswordEncoder uses a prefix in the stored password to determine which encoding method to apply, thereby supporting multiple encoding formats in a single application.
Understanding the Need for Secure Password Encoding
Why Encoding Matters
- Ir-reversibility: Unlike encryption, encoded passwords are one-way transformations.
- Storage Safety: Reduces the risk of password exposure even if the database is compromised.
- Compliance: Ensures that applications meet security standards required by most industries.
Comparison of Popular Encoding Strategies
| Encoding Type | Salting | Computational Expense | Recommended for Long-term Use |
| NoOpPasswordEncoder | No | Low | No |
| BCryptPasswordEncoder | Yes | Medium | Yes |
| SCryptPasswordEncoder | Yes | High | Yes |
| Pbkdf2PasswordEncoder | Yes | High | Yes |
| Argon2PasswordEncoder | Yes | High | Yes |
Important Notes
- Always utilize salted hashing to protect against rainbow table attacks.
- Keep updating the hash function as better algorithms become available.
- Store passwords as encoded values even for development or testing environments.
Conclusion
Choosing an effective password encoding strategy is integral to securing user credentials in any application. By leveraging Spring Security's PasswordEncoder, developers can protect sensitive data with minimal code, thereby ensuring that their applications adhere to modern security standards. Always choose a secure, efficient, and tested hashing strategy such as BCrypt or Argon2 for long-term assurance against unauthorized access.
Related reading
- Spring boot Kafka class deserialization - not in the trusted package
- Spring Boot MSSQL Kerberos Authentication
- Spring Boot Oauth2 client credentials
- Spring Boot project shows the Login page
- Spring Boot how to use multiple yml files
- Spring boot http response compression doesn't work for some User-Agents
- Spring Boot redirect HTTP to HTTPS
- Spring Boot Security CORS

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.