HashMapTwoFactorTokenManager implements TwoFactorTokenManager { @Override public TwoFactorToken getToken(String userId, String operation) { return new TwoFactorToken() { private static final long serialVersionUID = -5148037320548431456L; @Override public void generate(long timeout) { TwoFactorTokenInfo info = new TwoFactorTokenInfo(); info.timeOut = timeout; tokens.put(createTokenInfoKey(userId, operation), new WeakReference<>(info)); } @Override public boolean expired() { TwoFactorTokenInfo info = getTokenInfo(userId, operation); if (info == null) { return true; } if (info.isExpire()) { tokens.remove(createTokenInfoKey(userId, operation)); return true; } info.lastRequestTime = System.currentTimeMillis(); return false; } }; }  @Override TwoFactorToken getToken(String userId, String operation);  }
@Test @SneakyThrows public void test() { TwoFactorToken twoFactorToken = tokenManager.getToken("test", "test"); Assert.assertTrue(twoFactorToken.expired()); twoFactorToken.generate(1000L); Assert.assertFalse(twoFactorToken.expired()); Thread.sleep(1100); Assert.assertTrue(twoFactorToken.expired()); }