ExpiringUsernameAuthenticationToken extends UsernamePasswordAuthenticationToken { @Override public boolean isAuthenticated() { if (tokenExpiration != null && new Date().compareTo(tokenExpiration) >= 0) { return false; } else { return super.isAuthenticated(); } } ExpiringUsernameAuthenticationToken(Object principal, Object credentials);  ExpiringUsernameAuthenticationToken(Date tokenExpiration, Object principal, Object credentials, Collection<? extends GrantedAuthority> authorities); @Override boolean isAuthenticated(); Date getTokenExpiration(); @Override void eraseCredentials();  }
@Test public void testExpirationNull() throws Exception { ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(null, null, null, new LinkedList<GrantedAuthority>()); assertTrue(t.isAuthenticated()); }
@Test public void testExpirationFuture() throws Exception { Date future = new DateTime().plusHours(2).toDate(); ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(future, null, null, new LinkedList<GrantedAuthority>()); assertTrue(t.isAuthenticated()); }
@Test public void testExpirationFutureChange() throws Exception { Date future = new DateTime().plusMillis(1000).toDate(); ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(future, null, null, new LinkedList<GrantedAuthority>()); assertTrue(t.isAuthenticated()); synchronized (this) { wait(1000); } assertFalse(t.isAuthenticated()); }
@Test public void testNonAuthenticatedToken() throws Exception { ExpiringUsernameAuthenticationToken t = new ExpiringUsernameAuthenticationToken(null, null); assertFalse(t.isAuthenticated()); }