📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
The Importance of Password Encoding
Spring Security - PasswordEncoder
- BCryptPasswordEncoder - bcrypt
- Pbkdf2PasswordEncoder - PBKDF2
- SCryptPasswordEncoder - script
- Argon2PasswordEncoder - argon2
BCryptPasswordEncoder Implementation
- The BCryptPasswordEncoder implementation uses the widely supported bcrypt algorithm to hash the passwords.
- BCryptPasswordEncoder has the parameter strength. The default value in Spring Security is 10. Using a SecureRandom as a salt generator is recommended because it provides a cryptographically strong random number.
Argon2PasswordEncoder Implementation
- The Argon2PasswordEncoder implementation uses the Argon2 algorithm to hash the passwords.
- To defeat password cracking on custom hardware, Argon2 is a deliberately slow algorithm that requires large amounts of memory.
- The current implementation of the Argon2PasswordEncoder requires BouncyCastle.
Pbkdf2PasswordEncoder Implementation
- The Pbkdf2PasswordEncoder implementation uses the PBKDF2 algorithm to hash the passwords.
- To defeat password cracking, PBKDF2 is a deliberately slow algorithm.
- This algorithm is a good choice when FIPS certification is required.
SCryptPasswordEncoder Implementation
- The SCryptPasswordEncoder implementation uses the scrypt algorithm to hash the passwords.
- To defeat password cracking on custom hardware, scrypt is a deliberately slow algorithm that requires large amounts of memory.
Which is the most commonly used PasswordEncoder Implementation ?
Implementing Password Encoder in Spring Security
@Configuration
public class SpringSecurityConfig {
@Bean
public static PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeHttpRequests((authorize) -> {
authorize.anyRequest().authenticated();
}).httpBasic(Customizer.withDefaults());
return http.build();
}
@Bean
public UserDetailsService userDetailsService(){
UserDetails ramesh = User.builder()
.username("ramesh")
.password(passwordEncoder().encode("password"))
.roles("USER")
.build();
UserDetails admin = User.builder()
.username("admin")
.password(passwordEncoder().encode("admin"))
.roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(ramesh, admin);
}
}
In this example, we define a PasswordEncoder bean using the BCryptPasswordEncoder. When configuring the in-memory authentication, passwords are encoded using this encoder, enhancing the security of stored credentials.
Comments
Post a Comment
Leave Comment