Class DefaultPasswordService
java.lang.Object
org.apache.shiro.authc.credential.DefaultPasswordService
- All Implemented Interfaces:
HashingPasswordService,PasswordService
Default implementation of the
PasswordService interface that relies on an internal
HashService, HashFormat, and HashFormatFactory to function:
Hashing Passwords
Comparing Passwords
All hashing operations are performed by the internalhashService.- Since:
- 1.2
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new PasswordService with a default hash service and the default algorithm name "argon2id", a default hash format (shiro2) and a default hash format factory. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected org.apache.shiro.lang.util.ByteSourceprotected org.apache.shiro.crypto.hash.HashRequestcreateHashRequest(org.apache.shiro.lang.util.ByteSource plaintext) encryptPassword(Object plaintext) Converts the specified plaintext password (usually acquired from your application's 'new user' or 'password reset' workflow) into a formatted string safe for storage.org.apache.shiro.crypto.hash.format.HashFormatorg.apache.shiro.crypto.hash.format.HashFormatFactoryorg.apache.shiro.crypto.hash.HashServiceorg.apache.shiro.crypto.hash.HashhashPassword(Object plaintext) Hashes the specified plaintext password using internal hashing configuration settings pertinent to password hashing.booleanpasswordsMatch(Object submittedPlaintext, String saved) Returnstrueif thesubmittedPlaintextpassword matches the existingsavedpassword,falseotherwise.booleanpasswordsMatch(Object plaintext, org.apache.shiro.crypto.hash.Hash saved) Returnstrueif thesubmittedPlaintextpassword matches the existingsavedPasswordHash,falseotherwise.voidsetHashFormat(org.apache.shiro.crypto.hash.format.HashFormat hashFormat) voidsetHashFormatFactory(org.apache.shiro.crypto.hash.format.HashFormatFactory hashFormatFactory) voidsetHashService(org.apache.shiro.crypto.hash.HashService hashService)
-
Field Details
-
DEFAULT_HASH_ALGORITHM
-
-
Constructor Details
-
DefaultPasswordService
public DefaultPasswordService()Constructs a new PasswordService with a default hash service and the default algorithm name "argon2id", a default hash format (shiro2) and a default hash format factory.The default algorithm can change between minor versions and does not introduce API incompatibility by design.
-
-
Method Details
-
encryptPassword
Description copied from interface:PasswordServiceConverts the specified plaintext password (usually acquired from your application's 'new user' or 'password reset' workflow) into a formatted string safe for storage. The returned string can be safely saved with the corresponding user account record (e.g. as a 'password' attribute). It is expected that the String returned from this method will be presented to thepasswordsMatch(plaintext,encrypted)method when performing a password comparison check.Usage
The input argument type can be any 'byte backed'Object- almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported byByteSource.Util.isCompatible(Object)is valid. For example:String rawPassword = ... String encryptedValue = passwordService.encryptPassword(rawPassword);
or, identically:char[] rawPasswordChars = ... String encryptedValue = passwordService.encryptPassword(rawPasswordChars);
The resultingencryptedValueshould be stored with the account to be retrieved later during a login attempt. For example:String encryptedValue = passwordService.encryptPassword(rawPassword); ... userAccount.setPassword(encryptedValue); userAccount.save(); //create or update to your data store
- Specified by:
encryptPasswordin interfacePasswordService- Parameters:
plaintext- the raw password as 'byte-backed' object (String, character array,ByteSource, etc.) usually acquired from your application's 'new user' or 'password reset' workflow.- Returns:
- the encrypted password, formatted for storage.
- See Also:
-
hashPassword
Description copied from interface:HashingPasswordServiceHashes the specified plaintext password using internal hashing configuration settings pertinent to password hashing. Note that this method is only likely to be used in more complex environments that wish to format and/or save the returnedHashobject in a custom manner. Most applications will find theencryptPasswordmethod suitable enough for safety and ease-of-use.Usage
The input argument type can be any 'byte backed'Object- almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported byByteSource.Util.isCompatible(Object)is valid. Regardless of your choice of using Strings or character arrays to represent submitted passwords, you can wrap either as aByteSourceby usingByteSource.Util, for example, when the passwords are captured as Strings:ByteSource passwordBytes = ByteSource.Util.bytes(submittedPasswordString); Hash hashedPassword = hashingPasswordService.hashPassword(passwordBytes);
or, identically, when captured as a character array:ByteSource passwordBytes = ByteSource.Util.bytes(submittedPasswordCharacterArray); Hash hashedPassword = hashingPasswordService.hashPassword(passwordBytes);
- Specified by:
hashPasswordin interfaceHashingPasswordService- Parameters:
plaintext- the raw password as 'byte-backed' object (String, character array,ByteSource, etc.) usually acquired from your application's 'new user' or 'password reset' workflow.- Returns:
- the hashed password.
- See Also:
-
passwordsMatch
Description copied from interface:HashingPasswordServiceReturnstrueif thesubmittedPlaintextpassword matches the existingsavedPasswordHash,falseotherwise. Note that this method is only likely to be used in more complex environments that save hashes in a custom manner. Most applications will find thepasswordsMatch(plaintext,string)method sufficient ifencrypting passwords as Strings.Usage
ThesubmittedPlaintextargument type can be any 'byte backed'Object- almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported byByteSource.Util.isCompatible(Object)is valid.- Specified by:
passwordsMatchin interfaceHashingPasswordService- Parameters:
plaintext- a raw/plaintext password submitted by an end user/Subject.saved- the previously hashed password known to be associated with an account. This value is expected to have been previously generated from thehashPasswordmethod (typically when the account is created or the account's password is reset).- Returns:
trueif theplaintextpassword matches the existingsavedPasswordHash,falseotherwise.
-
checkHashFormatDurability
-
createHashRequest
protected org.apache.shiro.crypto.hash.HashRequest createHashRequest(org.apache.shiro.lang.util.ByteSource plaintext) -
createByteSource
-
passwordsMatch
Description copied from interface:PasswordServiceReturnstrueif thesubmittedPlaintextpassword matches the existingsavedpassword,falseotherwise.Usage
ThesubmittedPlaintextargument type can be any 'byte backed'Object- almost always either a String or character array representing passwords (character arrays are often a safer way to represent passwords as they can be cleared/nulled-out after use. Any argument type supported byByteSource.Util.isCompatible(Object)is valid. For example:String submittedPassword = ... passwordService.passwordsMatch(submittedPassword, encryptedPassword);
or similarly:char[] submittedPasswordCharacters = ... passwordService.passwordsMatch(submittedPasswordCharacters, encryptedPassword);
- Specified by:
passwordsMatchin interfacePasswordService- Parameters:
submittedPlaintext- a raw/plaintext password submitted by an end user/Subject.saved- the previously encrypted password known to be associated with an account. This value is expected to have been previously generated from theencryptPasswordmethod (typically when the account is created or the account's password is reset).- Returns:
trueif thesubmittedPlaintextpassword matches the existingsavedpassword,falseotherwise.- See Also:
-
getHashService
-
setHashService
-
getHashFormat
-
setHashFormat
-
getHashFormatFactory
-
setHashFormatFactory
public void setHashFormatFactory(org.apache.shiro.crypto.hash.format.HashFormatFactory hashFormatFactory)
-