Class Pbkdf2Password4jPasswordEncoder
java.lang.Object
org.springframework.security.crypto.password.AbstractValidatingPasswordEncoder
org.springframework.security.crypto.password4j.Pbkdf2Password4jPasswordEncoder
- All Implemented Interfaces:
PasswordEncoder
Implementation of
PasswordEncoder
that uses the Password4j library with PBKDF2 hashing algorithm.
PBKDF2 is a key derivation function designed to be computationally expensive to thwart dictionary and brute force attacks. This implementation handles the salt management explicitly since Password4j's PBKDF2 implementation does not include the salt in the output hash.
The encoded password format is: {salt}:{hash} where both salt and hash are Base64 encoded.
This implementation is thread-safe and can be shared across multiple threads.
Usage Examples:
// Using default PBKDF2 settings (recommended)
PasswordEncoder encoder = new Pbkdf2Password4jPasswordEncoder();
// Using custom PBKDF2 function
PasswordEncoder customEncoder = new Pbkdf2Password4jPasswordEncoder(
PBKDF2Function.getInstance(Algorithm.HMAC_SHA256, 100000, 256));
- Since:
- 7.0
- See Also:
-
PBKDF2FunctionAlgorithmFinder.getPBKDF2Instance()
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a PBKDF2 password encoder using the default PBKDF2 configuration from Password4j's AlgorithmFinder.Pbkdf2Password4jPasswordEncoder(com.password4j.PBKDF2Function pbkdf2Function) Constructs a PBKDF2 password encoder with a custom PBKDF2 function.Pbkdf2Password4jPasswordEncoder(com.password4j.PBKDF2Function pbkdf2Function, int saltLength) Constructs a PBKDF2 password encoder with a custom PBKDF2 function and salt length. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringencodeNonNullPassword(String rawPassword) protected booleanmatchesNonNull(String rawPassword, String encodedPassword) protected booleanupgradeEncodingNonNull(String encodedPassword) Methods inherited from class org.springframework.security.crypto.password.AbstractValidatingPasswordEncoder
encode, matches, upgradeEncoding
-
Constructor Details
-
Pbkdf2Password4jPasswordEncoder
public Pbkdf2Password4jPasswordEncoder()Constructs a PBKDF2 password encoder using the default PBKDF2 configuration from Password4j's AlgorithmFinder. -
Pbkdf2Password4jPasswordEncoder
public Pbkdf2Password4jPasswordEncoder(com.password4j.PBKDF2Function pbkdf2Function) Constructs a PBKDF2 password encoder with a custom PBKDF2 function.- Parameters:
pbkdf2Function- the PBKDF2 function to use for encoding passwords, must not be null- Throws:
IllegalArgumentException- if pbkdf2Function is null
-
Pbkdf2Password4jPasswordEncoder
public Pbkdf2Password4jPasswordEncoder(com.password4j.PBKDF2Function pbkdf2Function, int saltLength) Constructs a PBKDF2 password encoder with a custom PBKDF2 function and salt length.- Parameters:
pbkdf2Function- the PBKDF2 function to use for encoding passwords, must not be nullsaltLength- the length of the salt in bytes, must be positive- Throws:
IllegalArgumentException- if pbkdf2Function is null or saltLength is not positive
-
-
Method Details
-
encodeNonNullPassword
- Specified by:
encodeNonNullPasswordin classAbstractValidatingPasswordEncoder
-
matchesNonNull
- Specified by:
matchesNonNullin classAbstractValidatingPasswordEncoder
-
upgradeEncodingNonNull
- Overrides:
upgradeEncodingNonNullin classAbstractValidatingPasswordEncoder
-