Java Spring Security - User.withDefaultPasswordEncoder is deprecated?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
User.withDefaultPasswordEncoder() is deprecated because it stores a password encoder with the user details in source code, which is insecure for production. The method was intended only for demos and tests. The replacement is to define a PasswordEncoder bean separately and encode passwords explicitly. Spring Security recommends using BCryptPasswordEncoder (or Argon2/SCrypt) as a standalone bean and encoding passwords at registration time, not at user creation time.
The Deprecated Code
The deprecation warning:
Why It Is Deprecated
- Plaintext password in source code: The password string
"password"is visible in the source. The encoding happens in memory but the raw password is in the codebase. - Hardcoded encoder: The method uses
BCryptPasswordEncoderinternally, but you have no control over the encoder configuration (strength, algorithm). - Misleading API: Developers copy demo code into production, accidentally shipping in-memory users with weak passwords.
Fix 1: Separate PasswordEncoder Bean (Recommended)
Fix 2: Use {bcrypt} Prefix with DelegatingPasswordEncoder
Spring Security 5+ supports password storage format with prefixes:
PasswordEncoderFactories.createDelegatingPasswordEncoder() detects the prefix ({bcrypt}, {argon2}, {scrypt}, {noop}) and uses the appropriate encoder.
Generate BCrypt Hash for Hardcoded Users
Fix 3: Database-Backed UserDetailsService (Production)
For production, users should come from a database, not in-memory:
Registration Service
Fix 4: {noop} for Tests Only
For unit tests where you do not want hashing overhead:
{noop} tells the DelegatingPasswordEncoder to store and compare passwords as plaintext. Never use in production.
Common Pitfalls
- Using
withDefaultPasswordEncoderin production: The method is fine for quick demos but should never appear in production code. It stores the raw password in source code and provides no control over the encoding algorithm or strength. - Forgetting the
PasswordEncoderbean: Without aPasswordEncoderbean, Spring Security throwsThere is no PasswordEncoder mapped for the id "null". Always define aPasswordEncoderbean, even if it is justPasswordEncoderFactories.createDelegatingPasswordEncoder(). - Double-encoding passwords: If you call
passwordEncoder.encode(password)during registration AND the authentication flow also encodes, the password will never match. Encode once at registration, then the framework compares the raw login password against the stored hash. - Using MD5 or SHA-256 for password hashing: These are cryptographic hash functions, not password hashing functions. They are fast, which makes them vulnerable to brute-force attacks. Use BCrypt, Argon2, or SCrypt — these are intentionally slow.
- Hardcoding BCrypt hashes without the
{bcrypt}prefix: If usingDelegatingPasswordEncoder, stored passwords must have the algorithm prefix (e.g.,{bcrypt}$2a$...). Without the prefix, authentication fails withThere is no PasswordEncoder mapped for the id "null".
Summary
User.withDefaultPasswordEncoder()is deprecated — useUser.builder()with an explicitPasswordEncoderbean- Define
BCryptPasswordEncoderas a@Beanand call.encode()on passwords - Use
DelegatingPasswordEncoderwith{bcrypt}prefixes for flexibility - In production, store encoded passwords in a database and load them via
UserDetailsService - Use
{noop}prefix only in tests — never in production - Always define a
PasswordEncoderbean to avoid "no PasswordEncoder mapped" errors
Related reading
- Java sun.security.provider.certpath.SunCertPathBuilderException unable to find valid certification path to requested target
- java.lang.NoSuchMethodException sun.misc.Unsafe.defineClassjava.lang.String,B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain
- Jupyter notebook not trusted
- JWT decoding with Spring Security
- Java Stanford NLP Part of Speech labels?
- Java Static vs inner class
- JWT 'module' object has no attribute 'encode
- Kafka-topics --list using ssl

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.