Example usage for javax.security.auth.kerberos KerberosPrincipal KerberosPrincipal

List of usage examples for javax.security.auth.kerberos KerberosPrincipal KerberosPrincipal

Introduction

In this page you can find the example usage for javax.security.auth.kerberos KerberosPrincipal KerberosPrincipal.

Prototype


public KerberosPrincipal(String name, int nameType) 

Source Link

Document

Constructs a KerberosPrincipal from the provided string and name type input.

Usage

From source file:org.adeptnet.auth.kerberos.Krb5.java

public String isTicketValid(String spn, byte[] ticket) {
    checkCreds();/* w  w  w .  j a  v a  2s. c om*/
    LoginContext ctx = null;
    try {
        if (!config.getKeytab().exists()) {
            throw new LoginException(
                    String.format("KeyTab does not exist: %s", config.getKeytab().getAbsolutePath()));
        }
        final Principal principal = new KerberosPrincipal(spn, KerberosPrincipal.KRB_NT_SRV_INST);
        Set<Principal> principals = new HashSet<>();
        principals.add(principal);

        final Subject subject = new Subject(false, principals, new HashSet<>(), new HashSet<>());

        ctx = new LoginContext(config.getContextName(), subject, null, getJaasKrb5TicketCfg(spn));
        ctx.login();

        final Krb5TicketValidateAction validateAction = new Krb5TicketValidateAction(ticket, spn);
        final String username = Subject.doAs(subject, validateAction);
        return username;
    } catch (java.security.PrivilegedActionException | LoginException e) {
        LOG.fatal(spn, e);
    } finally {
        try {
            if (ctx != null) {
                ctx.logout();
            }
        } catch (LoginException e2) {
            LOG.fatal(spn, e2);
        }
    }

    return FAILED;
}