public class KeyBasedPersistenceTokenService extends java.lang.Object implements TokenService, org.springframework.beans.factory.InitializingBean
TokenServicethat is compatible with clusters and across machine restarts, without requiring database persistence.
Keys are produced in the format:
Base64(creationTime + ":" + hex(pseudoRandomNumber) + ":" + extendedInformation + ":" + Sha512Hex(creationTime + ":" + hex(pseudoRandomNumber) + ":" + extendedInformation + ":" + serverSecret) )
In the above,
extendedInformation are equal to that stored in
Sha512Hex includes the same payload, plus a
serverSecret varies every millisecond. It relies on two static
server-side secrets. The first is a password, and the second is a server integer. Both
of these must remain the same for any issued keys to subsequently be recognised. The
serverSecret in any millisecond is computed by
password + ":" + (
This approach further obfuscates the actual server secret and renders attempts to
compute the server secret more limited in usefulness (as any false tokens would be
forced to have a
creationTime equal to the computed hash). Recall that
framework features depending on token services should reject tokens that are relatively
old in any event.
A further consideration of this class is the requirement for cryptographically strong
pseudo-random numbers. To this end, the use of
recommended to inject the property.
This implementation uses UTF-8 encoding internally for string manipulation.
|Constructor and Description|
|Modifier and Type||Method and Description|
Forces the allocation of a new
Permits verification the
public Token allocateToken(java.lang.String extendedInformation)
extendedInformation- the extended information desired in the token (cannot be
null, but can be empty)
TokenService.verifyToken(String)at any future time.
public Token verifyToken(java.lang.String key)
Token.getKey()was issued by this
TokenServiceand reconstructs the corresponding
public void setServerSecret(java.lang.String serverSecret)
serverSecret- the new secret, which can contain a ":" if desired (never being sent to the client)
public void setSecureRandom(java.security.SecureRandom secureRandom)
public void setPseudoRandomNumberBytes(int pseudoRandomNumberBytes)
pseudoRandomNumberBytes- changes the number of bytes issued (must be >= 0; defaults to 256)
public void setServerInteger(java.lang.Integer serverInteger)
public void afterPropertiesSet()