Class AbstractRememberMeManager
java.lang.Object
org.apache.shiro.mgt.AbstractRememberMeManager
- All Implemented Interfaces:
RememberMeManager
Abstract implementation of the
RememberMeManager interface that handles
serialization and
encryption of the remembered user identity.
The remembered identity storage location and details are left to subclasses.
Default encryption key
This implementation uses anAesCipherService for strong encryption by default. It also
uses a default generated symmetric key to both encrypt and decrypt data. As AES is a symmetric cipher, the same
key is used to both encrypt and decrypt data, BUT NOTE:
Because Shiro is an open-source project, if anyone knew that you were using Shiro's default
key, they could download/view the source, and with enough effort, reconstruct the key
and decode encrypted data at will.
Of course, this key is only really used to encrypt the remembered PrincipalCollection which is typically
a user id or username. So if you do not consider that sensitive information, and you think the default key still
makes things 'sufficiently difficult', then you can ignore this issue.
However, if you do feel this constitutes sensitive information, it is recommended that you provide your own
key via the setCipherKey method to a key known only to your application,
guaranteeing that no third party can decrypt your data. You can generate your own key by calling the
CipherService's generateNewKey method
and using that result as the cipherKey configuration attribute.- Since:
- 0.9
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor that initializes aDefaultSerializeras theserializerand anAesCipherServiceas thecipherService.AbstractRememberMeManager(Supplier<byte[]> keySupplier) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected PrincipalCollectionconvertBytesToPrincipals(byte[] bytes, SubjectContext subjectContext) If acipherServiceis available, it will be used to first decrypt the byte array.protected byte[]convertPrincipalsToBytes(PrincipalCollection principals) Converts the given principal collection the byte array that will be persisted to be 'remembered' later.protected byte[]decrypt(byte[] encrypted) Decrypts the byte array using the configuredcipherService.protected PrincipalCollectiondeserialize(byte[] serializedIdentity) Deserializes the given byte array by using theserializer'sdeserializemethod.protected byte[]encrypt(byte[] serialized) Encrypts the byte array by using the configuredcipherService.protected abstract voidforgetIdentity(Subject subject) Forgets (removes) any remembered identity data for the specifiedSubjectinstance.byte[]Convenience method that returns the cipher key to use for both encryption and decryption.org.apache.shiro.crypto.cipher.CipherServiceReturns theCipherServiceto use for encrypting and decrypting serialized identity data to prevent easy inspection of Subject identity data.byte[]Returns the decryption cipher key to use for decryption operations.byte[]Returns the cipher key to use for encryption operations.protected PrincipalCollectiongetIdentityToRemember(Subject subject, AuthenticationInfo info) getRememberedPrincipals(SubjectContext subjectContext) Implements the interface method by firstacquiringthe remembered serialized byte array.protected abstract byte[]getRememberedSerializedIdentity(SubjectContext subjectContext) Based on the given subject context data, retrieves the previously persisted serialized identity, ornullif there is no available data.org.apache.shiro.lang.io.Serializer<PrincipalCollection> Returns theSerializerused to serialize and deserializePrincipalCollectioninstances for persistent remember me storage.protected booleanisRememberMe(AuthenticationToken token) Determines whether or not remember me services should be performed for the specified token.voidonFailedLogin(Subject subject, AuthenticationToken token, AuthenticationException ae) Reacts to a failed login by immediatelyforgettingany previously remembered identity.voidReacts to a subject logging out of the application and immediatelyforgetsany previously stored identity and returns.protected PrincipalCollectionCalled when an exception is thrown while trying to retrieve principals.voidonSuccessfulLogin(Subject subject, AuthenticationToken token, AuthenticationInfo info) Reacts to the successful login attempt by first alwaysforgettingany previously stored identity.voidrememberIdentity(Subject subject, AuthenticationToken token, AuthenticationInfo authcInfo) Remembers a subject-unique identity for retrieval later.protected voidrememberIdentity(Subject subject, PrincipalCollection accountPrincipals) Remembers the specified account principals by firstconvertingthem to a byte array and thenremembersthat byte array.protected abstract voidrememberSerializedIdentity(Subject subject, byte[] serialized) Persists the identity bytes to a persistent store for retrieval later via thegetRememberedSerializedIdentity(SubjectContext)method.protected byte[]serialize(PrincipalCollection principals) Serializes the givenprincipalsby serializing them to a byte array by using theserializer'sserializemethod.voidsetCipherKey(byte[] cipherKey) Convenience method that sets the cipher key to use for both encryption and decryption.voidsetCipherService(org.apache.shiro.crypto.cipher.CipherService cipherService) Sets theCipherServiceto use for encrypting and decrypting serialized identity data to prevent easy inspection of Subject identity data.voidsetDecryptionCipherKey(byte[] decryptionCipherKey) Sets the decryption key to use for decryption operations.voidsetEncryptionCipherKey(byte[] encryptionCipherKey) Sets the encryption key to use for encryption operations.voidsetSerializer(org.apache.shiro.lang.io.Serializer<PrincipalCollection> serializer) Sets theSerializerused to serialize and deserializePrincipalCollectioninstances for persistent remember me storage.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.shiro.mgt.RememberMeManager
forgetIdentity
-
Constructor Details
-
AbstractRememberMeManager
public AbstractRememberMeManager()Default constructor that initializes aDefaultSerializeras theserializerand anAesCipherServiceas thecipherService. -
AbstractRememberMeManager
Constructor. Pass keySupplier that supplies encryption key- Parameters:
keySupplier-- Since:
- 2.0
-
-
Method Details
-
getSerializer
Returns theSerializerused to serialize and deserializePrincipalCollectioninstances for persistent remember me storage. Unless overridden by thesetSerializer(org.apache.shiro.lang.io.Serializer<org.apache.shiro.subject.PrincipalCollection>)method, the default instance is aDefaultSerializer.- Returns:
- the
Serializerused to serialize and deserializePrincipalCollectioninstances for persistent remember me storage.
-
setSerializer
Sets theSerializerused to serialize and deserializePrincipalCollectioninstances for persistent remember me storage. Unless overridden by this method, the default instance is aDefaultSerializer.- Parameters:
serializer- theSerializerused to serialize and deserializePrincipalCollectioninstances for persistent remember me storage.
-
getCipherService
Returns theCipherServiceto use for encrypting and decrypting serialized identity data to prevent easy inspection of Subject identity data. Unless overridden by thesetCipherService(org.apache.shiro.crypto.cipher.CipherService)method, the default instance is anAesCipherService.- Returns:
- the
Cipherto use for encrypting and decrypting serialized identity data to prevent easy inspection of Subject identity data
-
setCipherService
Sets theCipherServiceto use for encrypting and decrypting serialized identity data to prevent easy inspection of Subject identity data. If the CipherService is a symmetric CipherService (using the same key for both encryption and decryption), you should set your key via thesetCipherKey(byte[])method. If the CipherService is an asymmetric CipherService (different keys for encryption and decryption, such as public/private key pairs), you should set your encryption and decryption key via the respectivesetEncryptionCipherKey(byte[])andsetDecryptionCipherKey(byte[])methods. N.B. Unless overridden by this method, the default CipherService instance is anAesCipherService. ThisRememberMeManagerimplementation already has a configured symmetric key to use for encryption and decryption, but it is recommended to provide your own for added security. See the class-level JavaDoc for more information and why it might be good to provide your own.- Parameters:
cipherService- theCipherServiceto use for encrypting and decrypting serialized identity data to prevent easy inspection of Subject identity data.
-
getEncryptionCipherKey
Returns the cipher key to use for encryption operations.- Returns:
- the cipher key to use for encryption operations.
- See Also:
-
setEncryptionCipherKey
Sets the encryption key to use for encryption operations.- Parameters:
encryptionCipherKey- the encryption key to use for encryption operations.- See Also:
-
getDecryptionCipherKey
Returns the decryption cipher key to use for decryption operations.- Returns:
- the cipher key to use for decryption operations.
- See Also:
-
setDecryptionCipherKey
Sets the decryption key to use for decryption operations.- Parameters:
decryptionCipherKey- the decryption key to use for decryption operations.- See Also:
-
getCipherKey
Convenience method that returns the cipher key to use for both encryption and decryption. N.B. This method can only be called if the underlyingcipherServiceis a symmetric CipherService which by definition uses the same key for both encryption and decryption. If using an asymmetric CipherService public/private key pair, you cannot use this method, and should instead use thegetEncryptionCipherKey()andgetDecryptionCipherKey()methods individually. The defaultAesCipherServiceinstance is a symmetric cipher service, so this method can be used if you are using the default.- Returns:
- the symmetric cipher key used for both encryption and decryption.
-
setCipherKey
Convenience method that sets the cipher key to use for both encryption and decryption. N.B. This method can only be called if the underlyingcipherServiceis a symmetric CipherService?which by definition uses the same key for both encryption and decryption. If using an asymmetric CipherService?(such as a public/private key pair), you cannot use this method, and should instead use thesetEncryptionCipherKey(byte[])andsetDecryptionCipherKey(byte[])methods individually. The defaultAesCipherServiceinstance is a symmetric CipherService, so this method can be used if you are using the default.- Parameters:
cipherKey- the symmetric cipher key to use for both encryption and decryption.
-
forgetIdentity
Forgets (removes) any remembered identity data for the specifiedSubjectinstance.- Parameters:
subject- the subject instance for which identity data should be forgotten from the underlying persistence mechanism.
-
isRememberMe
Determines whether or not remember me services should be performed for the specified token. This method returnstrueiff:- The token is not
nulland - The token is an
instanceofRememberMeAuthenticationTokenand token.isRememberMe()istrue
- Parameters:
token- the authentication token submitted during the successful authentication attempt.- Returns:
- true if remember me services should be performed as a result of the successful authentication attempt.
- The token is not
-
onSuccessfulLogin
Reacts to the successful login attempt by first alwaysforgettingany previously stored identity. Then if thetokenis a RememberMetoken, the associated identity will berememberedfor later retrieval during a new user session.- Specified by:
onSuccessfulLoginin interfaceRememberMeManager- Parameters:
subject- the subject for which the principals are being remembered.token- the token that resulted in a successful authentication attempt.info- the authentication info resulting from the successful authentication attempt.
-
rememberIdentity
public void rememberIdentity(Subject subject, AuthenticationToken token, AuthenticationInfo authcInfo) Remembers a subject-unique identity for retrieval later. This implementation firstresolvesthe exactprincipalsto remember. It then remembers the principals by callingrememberIdentity(org.apache.shiro.subject.Subject, org.apache.shiro.subject.PrincipalCollection). This implementation ignores theAuthenticationTokenargument, but it is available to subclasses if necessary for custom logic.- Parameters:
subject- the subject for which the principals are being remembered.token- the token that resulted in a successful authentication attempt.authcInfo- the authentication info resulting from the successful authentication attempt.
-
getIdentityToRemember
- Parameters:
subject- the subject for which the principals are being remembered.info- the authentication info resulting from the successful authentication attempt.- Returns:
- the
PrincipalCollectionto remember.
-
rememberIdentity
Remembers the specified account principals by firstconvertingthem to a byte array and thenremembersthat byte array.- Parameters:
subject- the subject for which the principals are being remembered.accountPrincipals- the principals to remember for retrieval later.
-
convertPrincipalsToBytes
Converts the given principal collection the byte array that will be persisted to be 'remembered' later. This implementation firstserializesthe principals to a byte array and thenencryptsthat byte array.- Parameters:
principals- thePrincipalCollectionto convert to a byte array- Returns:
- the representative byte array to be persisted for remember me functionality.
-
rememberSerializedIdentity
Persists the identity bytes to a persistent store for retrieval later via thegetRememberedSerializedIdentity(SubjectContext)method.- Parameters:
subject- the Subject for which the identity is being serialized.serialized- the serialized bytes to be persisted.
-
getRememberedPrincipals
Implements the interface method by firstacquiringthe remembered serialized byte array. Then itconvertsthem and returns the re-constitutedPrincipalCollection. If no remembered principals could be obtained,nullis returned. If any exceptions are thrown, theonRememberedPrincipalFailure(RuntimeException, SubjectContext)method is called to allow any necessary post-processing (such as immediately removing any previously remembered values for safety).- Specified by:
getRememberedPrincipalsin interfaceRememberMeManager- Parameters:
subjectContext- the contextual data, usually provided by aSubject.Builderimplementation, that is being used to construct aSubjectinstance.- Returns:
- the remembered principals or
nullif none could be acquired.
-
getRememberedSerializedIdentity
Based on the given subject context data, retrieves the previously persisted serialized identity, ornullif there is no available data. The context map is usually populated by aSubject.Builderimplementation. See theSubjectFactoryclass constants for Shiro's known map keys.- Parameters:
subjectContext- the contextual data, usually provided by aSubject.Builderimplementation, that is being used to construct aSubjectinstance. To be used to assist with data lookup.- Returns:
- the previously persisted serialized identity, or
nullif there is no available data for the Subject.
-
convertBytesToPrincipals
If acipherServiceis available, it will be used to first decrypt the byte array. Then the bytes are thendeserializedand then returned.- Parameters:
bytes- the bytes to decrypt if necessary and then deserialize.subjectContext- the contextual data, usually provided by aSubject.Builderimplementation, that is being used to construct aSubjectinstance.- Returns:
- the deserialized and possibly decrypted principals
-
onRememberedPrincipalFailure
protected PrincipalCollection onRememberedPrincipalFailure(RuntimeException e, SubjectContext context) Called when an exception is thrown while trying to retrieve principals. The default implementation logs a warning message and forgets ('unremembers') the problem identity by callingforgetIdentity(context)and then immediately re-throws the exception to allow the calling component to react accordingly. This method implementation never returns an object - it always rethrows, but can be overridden by subclasses for custom handling behavior. This most commonly would be called when an encryption key is updated and old principals are retrieved that have been encrypted with the previous key.- Parameters:
e- the exception that was thrown.context- the contextual data, usually provided by aSubject.Builderimplementation, that is being used to construct aSubjectinstance.- Returns:
- nothing - the original
RuntimeExceptionis propagated in all cases.
-
encrypt
Encrypts the byte array by using the configuredcipherService.- Parameters:
serialized- the serialized object byte array to be encrypted- Returns:
- an encrypted byte array returned by the configured
() cipher.
-
decrypt
Decrypts the byte array using the configuredcipherService.- Parameters:
encrypted- the encrypted byte array to decrypt- Returns:
- the decrypted byte array returned by the configured
() cipher.
-
serialize
Serializes the givenprincipalsby serializing them to a byte array by using theserializer'sserializemethod.- Parameters:
principals- the principal collection to serialize to a byte array- Returns:
- the serialized principal collection in the form of a byte array
-
deserialize
Deserializes the given byte array by using theserializer'sdeserializemethod.- Parameters:
serializedIdentity- the previously serializedPrincipalCollectionas a byte array- Returns:
- the deserialized (reconstituted)
PrincipalCollection
-
onFailedLogin
Reacts to a failed login by immediatelyforgettingany previously remembered identity. This is an additional security feature to prevent any remnant identity data from being retained in case the authentication attempt is not being executed by the expected user.- Specified by:
onFailedLoginin interfaceRememberMeManager- Parameters:
subject- the subject which executed the failed login attempttoken- the authentication token resulting in a failed login attempt - ignored by this implementationae- the exception thrown as a result of the failed login attempt - ignored by this implementation
-
onLogout
Reacts to a subject logging out of the application and immediatelyforgetsany previously stored identity and returns.- Specified by:
onLogoutin interfaceRememberMeManager- Parameters:
subject- the subject logging out.
-