code: public @Nonnull
      UserDetails getUserDetailsForImpersonation() throws UsernameNotFoundException {
          ImpersonatingUserDetailsService userDetailsService = new ImpersonatingUserDetailsService(
                  Jenkins.get().getSecurityRealm().getSecurityComponents().userDetails
          );
  
          try {
              UserDetails userDetails = userDetailsService.loadUserByUsername(id);
              LOGGER.log(Level.FINE, "Impersonation of the user {0} was a success", id);
              return userDetails;
          } catch (UserMayOrMayNotExistException e) {
              LOGGER.log(Level.FINE, "The user {0} may or may not exist in the SecurityRealm, so we provide minimum access", id);
          } catch (UsernameNotFoundException e) {
              if (ALLOW_NON_EXISTENT_USER_TO_LOGIN) {
                  LOGGER.log(Level.FINE, "The user {0} was not found in the SecurityRealm but we are required to let it pass, due to ALLOW_NON_EXISTENT_USER_TO_LOGIN", id);
              } else {
                  LOGGER.log(Level.FINE, "The user {0} was not found in the SecurityRealm", id);
                  throw e;
              }
          } catch (DataAccessException e) {
              // seems like it's in the same boat as UserMayOrMayNotExistException
              LOGGER.log(Level.FINE, "The user {0} retrieval just threw a DataAccess exception with msg = {1}, so we provide minimum access", new Object[]{id, e.getMessage()});
          }
  
          return new LegitimateButUnknownUserDetails(id);
      }
docs: This method checks with {@link SecurityRealm} if the user is a valid user that can login to the security realm.
  If {@link SecurityRealm} is a kind that does not support querying information about other users, this will
  use {@link LastGrantedAuthoritiesProperty} to pick up the granted authorities as of the last time the user has
  logged in.
  
  @return userDetails for the user, in case he's not found but seems legitimate, we provide a userDetails with minimum access
  @throws UsernameNotFoundException If this user is not a valid user in the backend {@link SecurityRealm}.