code: private Principal getUserPrincipal(Subject subject) {
          Principal userPrincipal = null;
  
          Set<Principal> principals = subject.getPrincipals();
  
          // try and get userPrincipal based on userClassNames
          if (userClassNames != null && userClassNames.size() > 0) {
              for (Principal p : principals) {
                  if (userPrincipal == null
                          && userClassNames.contains(p.getClass().getName())) {
                      userPrincipal = p;
                  }
              }
          }
  
          // no userPrincipal found using userClassNames, just grab first principal
          if (userPrincipal == null) {
              Iterator<Principal> i = principals.iterator();
              // should always have 1 at least and 1st should be user principal
              if (i.hasNext()) {
                  userPrincipal = i.next();
              }
          }
  
          if (logger.isDebugEnabled()) {
              logger.debug("found userPrincipal [{}]: {}",
                           userPrincipal.getClass().getName(),
                           userPrincipal.getName());
          }
  
          return userPrincipal;
      }
docs: Given a subject, obtain the userPrincipal from it. The user principal is
  defined by a Principal class that can be defined in the web.xml file. If
  this is undefined, the first principal found is assumed to be the
  userPrincipal.
  
  @param subject
  the subject returned from authentication.
  @return the userPrincipal associated with the given subject.