code: public static GSSCredential getGSSCredentialFromSubject(final Subject subject) {
          if (subject == null) {
              return null;
          }
          GSSCredential gssCredential = AccessController.doPrivileged(new PrivilegedAction<GSSCredential>() {
              @Override
              public GSSCredential run() {
                  GSSCredential gssCredInSubject = null;
                  Set<GSSCredential> privCredentials = subject.getPrivateCredentials(GSSCredential.class);
                  if (privCredentials != null) {
                      Iterator<GSSCredential> privCredItr = privCredentials.iterator();
                      if (privCredItr.hasNext()) {
                          gssCredInSubject = privCredItr.next();
                      }
                  }
                  return gssCredInSubject;
              }
          });
  
          if (gssCredential == null) {
              KerberosTicket tgt = getKerberosTicketFromSubject(subject, null);
              if (tgt != null) {
                  gssCredential = createGSSCredential(subject, tgt);
              }
          }
  
          return gssCredential;
      }
docs: Gets a GSSCredential from a Subject