diff --git a/core/src/main/java/org/springframework/security/ui/rememberme/TokenBasedRememberMeServices.java b/core/src/main/java/org/springframework/security/ui/rememberme/TokenBasedRememberMeServices.java index a9c6c2133d..31085daff0 100644 --- a/core/src/main/java/org/springframework/security/ui/rememberme/TokenBasedRememberMeServices.java +++ b/core/src/main/java/org/springframework/security/ui/rememberme/TokenBasedRememberMeServices.java @@ -158,12 +158,13 @@ public class TokenBasedRememberMeServices extends AbstractRememberMeServices { return; } - long expiryTime = System.currentTimeMillis() + getTokenValiditySeconds() * 1000; + int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication); + long expiryTime = System.currentTimeMillis() + 1000*tokenLifetime; - String signatureValue = makeTokenSignature(expiryTime, username, password); + String signatureValue = makeTokenSignature(expiryTime, username, password); String cookieValue = encodeCookie(new String[] {username, Long.toString(expiryTime), signatureValue}); - response.addCookie(makeValidCookie(cookieValue, request, getTokenValiditySeconds())); + response.addCookie(makeValidCookie(cookieValue, request, tokenLifetime)); if (logger.isDebugEnabled()) { logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" @@ -171,7 +172,28 @@ public class TokenBasedRememberMeServices extends AbstractRememberMeServices { } } - protected String retrieveUserName(Authentication authentication) { + /** + * Calculates the validity period in seconds for a newly generated remember-me login. + * After this period (from the current time) the remember-me login will be considered expired. + * This method allows customization based on request parameters supplied with the login or information in + * the Authentication object. The default value is just the token validity period property, + * tokenValiditySeconds. + *

+ * The returned value will be used to work out the expiry time of the token and will also be + * used to set the maxAge property of the cookie. + *

+ * + * See SEC-485. + * + * @param request the request passed to onLoginSuccess + * @param authentication the successful authentication object. + * @return the lifetime in seconds. + */ + protected int calculateLoginLifetime(HttpServletRequest request, Authentication authentication) { + return getTokenValiditySeconds(); + } + + protected String retrieveUserName(Authentication authentication) { if (isInstanceOfUserDetails(authentication)) { return ((UserDetails) authentication.getPrincipal()).getUsername(); }