@Test public void testOnLoginSuccessEmptyUsername() { final UserDetails userDetails = context.mock(UserDetails.class); final PersistentLoginRepository loginRepo = context .mock(PersistentLoginRepository.class); final UserDetailsService userDetailsService = context .mock(UserDetailsService.class); final HttpServletRequest request = context .mock(HttpServletRequest.class); final HttpServletResponse response = context .mock(HttpServletResponse.class); final Authentication auth = context.mock(Authentication.class); context.checking(new Expectations() { { exactly(2).of(auth).getPrincipal(); will(returnValue(userDetails)); oneOf(userDetails).getUsername(); will(returnValue("")); } }); PersistentLoginService svc = new PersistentLoginService("key", userDetailsService, loginRepo); svc.onLoginSuccess(request, response, auth); context.assertIsSatisfied(); }
public void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication successfulAuthentication) { String username = retrieveUserName(successfulAuthentication); if (!StringUtils.hasLength(username)) { logger.error( "Authentication contains empty username. Unable to create persistent login info."); return; } int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication); long expiryTime = System.currentTimeMillis() + EXPIRE_TIME * tokenLifetime; String signatureValue = makeTokenSignature(expiryTime, username, UUID.randomUUID().toString()); try { PersistentLogin login = new PersistentLogin(username, signatureValue, expiryTime); loginRepository.createOrUpdatePersistentLogin(login); } catch (Exception e) { logger.error("Unable to insert PersistentLogin information to DB for user: " + username + "', expiry: '" + new Date(expiryTime) + "'"); return; } setCookie(new String[] { username, Long.toString(expiryTime), signatureValue }, tokenLifetime, request, response); if (logger.isDebugEnabled()) { logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'"); } }
PersistentLoginService extends TokenBasedRememberMeServices { public void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication successfulAuthentication) { String username = retrieveUserName(successfulAuthentication); if (!StringUtils.hasLength(username)) { logger.error( "Authentication contains empty username. Unable to create persistent login info."); return; } int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication); long expiryTime = System.currentTimeMillis() + EXPIRE_TIME * tokenLifetime; String signatureValue = makeTokenSignature(expiryTime, username, UUID.randomUUID().toString()); try { PersistentLogin login = new PersistentLogin(username, signatureValue, expiryTime); loginRepository.createOrUpdatePersistentLogin(login); } catch (Exception e) { logger.error("Unable to insert PersistentLogin information to DB for user: " + username + "', expiry: '" + new Date(expiryTime) + "'"); return; } setCookie(new String[] { username, Long.toString(expiryTime), signatureValue }, tokenLifetime, request, response); if (logger.isDebugEnabled()) { logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'"); } } }
PersistentLoginService extends TokenBasedRememberMeServices { public void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication successfulAuthentication) { String username = retrieveUserName(successfulAuthentication); if (!StringUtils.hasLength(username)) { logger.error( "Authentication contains empty username. Unable to create persistent login info."); return; } int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication); long expiryTime = System.currentTimeMillis() + EXPIRE_TIME * tokenLifetime; String signatureValue = makeTokenSignature(expiryTime, username, UUID.randomUUID().toString()); try { PersistentLogin login = new PersistentLogin(username, signatureValue, expiryTime); loginRepository.createOrUpdatePersistentLogin(login); } catch (Exception e) { logger.error("Unable to insert PersistentLogin information to DB for user: " + username + "', expiry: '" + new Date(expiryTime) + "'"); return; } setCookie(new String[] { username, Long.toString(expiryTime), signatureValue }, tokenLifetime, request, response); if (logger.isDebugEnabled()) { logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'"); } } PersistentLoginService(final String key,
            final UserDetailsService userDetailsService,
            final PersistentLoginRepository persistentLoginRepository); }
PersistentLoginService extends TokenBasedRememberMeServices { public void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication successfulAuthentication) { String username = retrieveUserName(successfulAuthentication); if (!StringUtils.hasLength(username)) { logger.error( "Authentication contains empty username. Unable to create persistent login info."); return; } int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication); long expiryTime = System.currentTimeMillis() + EXPIRE_TIME * tokenLifetime; String signatureValue = makeTokenSignature(expiryTime, username, UUID.randomUUID().toString()); try { PersistentLogin login = new PersistentLogin(username, signatureValue, expiryTime); loginRepository.createOrUpdatePersistentLogin(login); } catch (Exception e) { logger.error("Unable to insert PersistentLogin information to DB for user: " + username + "', expiry: '" + new Date(expiryTime) + "'"); return; } setCookie(new String[] { username, Long.toString(expiryTime), signatureValue }, tokenLifetime, request, response); if (logger.isDebugEnabled()) { logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'"); } } PersistentLoginService(final String key,
            final UserDetailsService userDetailsService,
            final PersistentLoginRepository persistentLoginRepository); UserDetails processAutoLoginCookie(final String[] cookieTokens,
            final HttpServletRequest request, final HttpServletResponse response); void onLoginSuccess(final HttpServletRequest request,
            final HttpServletResponse response,
            final Authentication successfulAuthentication); void logout(final HttpServletRequest request,
            final HttpServletResponse response, final Authentication authentication); }
PersistentLoginService extends TokenBasedRememberMeServices { public void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication successfulAuthentication) { String username = retrieveUserName(successfulAuthentication); if (!StringUtils.hasLength(username)) { logger.error( "Authentication contains empty username. Unable to create persistent login info."); return; } int tokenLifetime = calculateLoginLifetime(request, successfulAuthentication); long expiryTime = System.currentTimeMillis() + EXPIRE_TIME * tokenLifetime; String signatureValue = makeTokenSignature(expiryTime, username, UUID.randomUUID().toString()); try { PersistentLogin login = new PersistentLogin(username, signatureValue, expiryTime); loginRepository.createOrUpdatePersistentLogin(login); } catch (Exception e) { logger.error("Unable to insert PersistentLogin information to DB for user: " + username + "', expiry: '" + new Date(expiryTime) + "'"); return; } setCookie(new String[] { username, Long.toString(expiryTime), signatureValue }, tokenLifetime, request, response); if (logger.isDebugEnabled()) { logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'"); } } PersistentLoginService(final String key,
            final UserDetailsService userDetailsService,
            final PersistentLoginRepository persistentLoginRepository); UserDetails processAutoLoginCookie(final String[] cookieTokens,
            final HttpServletRequest request, final HttpServletResponse response); void onLoginSuccess(final HttpServletRequest request,
            final HttpServletResponse response,
            final Authentication successfulAuthentication); void logout(final HttpServletRequest request,
            final HttpServletResponse response, final Authentication authentication);  }