public static <T> T loginWithKerberos(
            Configuration configuration,
            String krb5FilePath,
            String kerberosPrincipal,
            String kerberosKeytabPath,
            LoginFunction<T> action)
            throws IOException, InterruptedException {
        if (!configuration.get("hadoop.security.authentication").equals("kerberos")) {
            throw new IllegalArgumentException("hadoop.security.authentication must be kerberos");
        }
        // Use global lock to avoid multiple threads to execute setConfiguration at the same time
        synchronized (UserGroupInformation.class) {
            if (StringUtils.isNotEmpty(krb5FilePath)) {
                System.setProperty("java.security.krb5.conf", krb5FilePath);
            }
            // init configuration
            UserGroupInformation.setConfiguration(configuration);
            UserGroupInformation userGroupInformation =
                    UserGroupInformation.loginUserFromKeytabAndReturnUGI(
                            kerberosPrincipal, kerberosKeytabPath);
            return userGroupInformation.doAs(
                    (PrivilegedExceptionAction<T>)
                            () -> action.run(configuration, userGroupInformation));
        }
    }
@Test
    void loginWithKerberos_success() throws Exception {
        miniKdc.createPrincipal(new File(workDir, "tom.keytab"), "tom");

        UserGroupInformation userGroupInformation =
                HadoopLoginFactory.loginWithKerberos(
                        createConfiguration(),
                        null,
                        "tom",
                        workDir.getPath() + "/" + "tom.keytab",
                        (conf, ugi) -> ugi);

        assertNotNull(userGroupInformation);
        assertEquals("tom@EXAMPLE.COM", userGroupInformation.getUserName());
    }