public final class DefaultPasswordEncoder extends Object implements PasswordEncoder
It is strongly recommended that clients use the default implementation returned
by getDefaultInstance(), which uses PKCS5S2PasswordHashGenerator
with RandomSaltGenerator.
The storage format used by this class is "{" + identifier + "}" + encodedSaltAndHash,
where identifier and saltPlusHash are defined as follows:
identifier: the identifier string provided to the constructorencodedSaltAndHash: the result of new String(encodeBase64(saltAndHash), "UTF-8")encodeBase64: the result of encodeBase64(saltAndHash)saltAndHash: the result of ArrayUtils.add(salt, hash)salt: the result of SaltGenerator.generateSalt(int)hash: the result of passwordHashGenerator.generateHash(password.getBytes("UTF-8"), salt)
Clients must provide an identifier, hash generator and salt generator in the
constructor, or use the default implementation returned by getDefaultInstance().
The thread-safety of this encoder depends on the thread-safety of the hash and salt generators
used. The encoder returned by getDefaultInstance() is safe for use on multiple threads.
Base64.encodeBase64(byte[]),
PasswordHashGenerator,
SaltGenerator| Constructor and Description |
|---|
DefaultPasswordEncoder(String identifier,
PasswordHashGenerator hashGenerator,
SaltGenerator saltGenerator)
Constructs a new encoder with specified identifier, hash generator and salt generator.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
canDecodePassword(String encodedPassword)
Returns true if the encodedPassword is in the right format for decoding and
verification by this implementation, otherwise false.
|
String |
encodePassword(String rawPassword)
Encodes a password and returns it as a String suitable for storage by the client.
|
static PasswordEncoder |
getDefaultInstance()
Returns a new encoder with identifier "PKCS5S2" using
PKCS5S2PasswordHashGenerator
as the hash generator and RandomSaltGenerator as the salt generator. |
boolean |
isValidPassword(String rawPassword,
String prefixedEncodedPassword)
Returns true if the rawPassword matches the stored password hash in
encodedPassword, otherwise false.
|
static PasswordEncoder |
newInstance(String identifier,
PasswordHashGenerator hashGenerator)
Returns a new encoder with specified identifier and hash generator, using
RandomSaltGenerator
as the salt generator. |
public DefaultPasswordEncoder(String identifier, PasswordHashGenerator hashGenerator, SaltGenerator saltGenerator)
The thread-safety of this instance depends on the thread-safety of the hash and salt generator implementations.
public static PasswordEncoder getDefaultInstance()
PKCS5S2PasswordHashGenerator
as the hash generator and RandomSaltGenerator as the salt generator.
This instance is safe for use by multiple threads.
PKCS5S2PasswordHashGenerator,
RandomSaltGeneratorpublic static PasswordEncoder newInstance(String identifier, PasswordHashGenerator hashGenerator)
RandomSaltGenerator
as the salt generator.
The thread-safety of this instance depends on the thread-safety of the hash generator implementation.
public final boolean canDecodePassword(String encodedPassword)
PasswordEncodercanDecodePassword in interface PasswordEncoderencodedPassword - the stored password associated with this userpublic final String encodePassword(String rawPassword) throws IllegalArgumentException
PasswordEncoderImplementations must perform a one-way hashing operation on the rawPassword so that the rawPassword cannot practically be derived from the encoded result by an attacker.
It is recommended that implementations include a unique prefix in their encoded form
which will allow PasswordEncoder.canDecodePassword(String) to be implemented easily.
encodePassword in interface PasswordEncoderrawPassword - the password provided by the userIllegalArgumentException - if the rawPassword is null or emptypublic final boolean isValidPassword(String rawPassword, String prefixedEncodedPassword) throws IllegalArgumentException
PasswordEncoderPasswordEncoder.encodePassword(String). If the encoded password
is not in a format which is handled by this encoder, this method will return false.
If multiple encodings are supported by an application, the client should call
PasswordEncoder.canDecodePassword(String) to check that the password was generated by
this encoder before calling this method.
isValidPassword in interface PasswordEncoderrawPassword - the raw password provided by the user for authenticationprefixedEncodedPassword - the stored password associated with the userIllegalArgumentException - if either rawPassword or encodedPassword is null or emptyCopyright © 2017 Atlassian. All rights reserved.