@Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    if (LOG.isDebugEnabled()) LOG.debug("Looking for user by uniqueId " + username);

    if (StringUtils.isEmpty(username)) return null;

    Cache.ValueWrapper wrapper = cacheService.get(CacheName.IDENTITY, username);

    UserDetails userDetails = null;

    if (wrapper != null) {
      userDetails = (UserDetails) wrapper.get();
      if (userDetails == null) throw new UsernameNotFoundException(username);

      return userDetails;
    }

    if (LOG.isDebugEnabled()) LOG.debug("Retrieving user by uniqueId " + username);

    try {
      userDetails = super.loadUserByUsername(username);

    } finally {
      cacheService.put(CacheName.IDENTITY, username, userDetails);
    }
    return userDetails;
  }
  private <T, E extends Throwable> T getValue(Callback<T, E> callback, Cache cache, Object key)
      throws E {
    @Nullable final Cache.ValueWrapper valueWrapper = cache.get(key);
    if (valueWrapper != null) {
      return cast(valueWrapper.get());
    }

    final T loadedValue = callback.call();
    putValue(cache, key, loadedValue);

    return loadedValue;
  }
  public UserDetails getUserFromCache(String username) {
    Cache.ValueWrapper element = username != null ? cache.get(username) : null;

    if (logger.isDebugEnabled()) {
      logger.debug("Cache hit: " + (element != null) + "; username: " + username);
    }

    if (element == null) {
      return null;
    } else {
      return (UserDetails) element.get();
    }
  }
  @Override
  public CsrfToken loadToken(HttpServletRequest request) {

    String token = Strings.clean(request.getParameter(parameterName));

    if (token == null) {
      token = Strings.clean(request.getHeader(headerName));
    }

    if (token == null) {
      return null;
    }

    try {
      Jws<Claims> jws = Jwts.parser().setSigningKey(signingKey).parseClaimsJws(token);

      // signature is valid, now let's ensure it hasn't been submitted before:

      String id = jws.getBody().getId();

      String usedNonce = null;

      Cache.ValueWrapper wrapper = nonceCache.get(id);
      if (wrapper != null) {
        Object val = wrapper.get();
        if (val != null) {
          usedNonce = val.toString();
        }
      }

      if (usedNonce == null) {
        // CSRF token hasn't been used yet, mark it as used:
        nonceCache.put(id, token);

        return new DefaultCsrfToken(headerName, parameterName, token);
      }
    } catch (Exception e) {
      log.debug(
          "CSRF token is invalid (this is likely to happen and not necessarily an error condition).",
          e);
    }

    return null;
  }
 @Override
 public T get(Object key) {
     Cache.ValueWrapper wrapper = cache.get(key);
     return wrapper == null ? null : (T)wrapper.get();
 }