code: protected Object getAuthenticationCacheKey(PrincipalCollection principals) {
          if (!CollectionUtils.isEmpty(principals)) {
              Collection thisPrincipals = principals.fromRealm(getName());
              if (CollectionUtils.isEmpty(thisPrincipals)) {
                  //no principals attributed to this particular realm.  Fall back to the 'master' primary:
                  return principals.getPrimaryPrincipal();
  
              } else {
                  Iterator iterator = thisPrincipals.iterator();
                  iterator.next(); //First item is the Stormpath' account href
                  //Second item is Stormpath' account map
                  Map<String, Object> accountInfo = (Map<String, Object>) iterator.next();
                  //Users can indistinctively login using their emails or usernames. Therefore, we need to try which is
                  //the key used in each case
                  String email = (String) accountInfo.get("email");
                  if (getAuthenticationCache().get(email) != null) {
                      return email;
                  }
                  return accountInfo.get("username");
              }
          }
          return null;
      }
docs: If authentication caching is enabled, authentication data for an account must be evicted (removed) from the cached during logout.
  <p/>
  Since the user submitted username is different to the the primary account identifier (i.e. username for the former, account href for the latter),
  we need to overwrite the {@link org.apache.shiro.realm.AuthenticatingRealm#getAuthenticationCacheKey(org.apache.shiro.subject.PrincipalCollection)
  AuthenticatingRealm#getAuthenticationCacheKey(PrincipalCollection)}.
  <p/>
  This guarantees that the same cache key used to cache the data during authentication (derived from the AuthenticationToken)
  will be used to remove the cached data during logout (derived from the PrincipalCollection).
  <p/>
  This is a fix for <a href="https://github.com/stormpath/stormpath-shiro/issues/6">Issue #6</a>.
  
  @param principals the collection of all principals associated with the current subject.
  @return the key used to store the authentication information in the cache (i.e. Stormpath's {@link Account} email)
  @since 0.6.0