Java X500Principal serverLogin(final String serverPrincipal, final String serverPassword)

Here you can find the source of serverLogin(final String serverPrincipal, final String serverPassword)

Description

Create server side Kerberos login context for provided credentials.

License

Open Source License

Parameter

Parameter Description
serverPrincipal server principal
serverPassword server passsword

Exception

Parameter Description
LoginException on error

Return

LoginContext server login context

Declaration

public static LoginContext serverLogin(final String serverPrincipal,
        final String serverPassword) throws LoginException 

Method Source Code

//package com.java2s;
/*//from   w  w  w. j  a  v a  2s . c  o m
 * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
 * Copyright (C) 2012  Mickael Guessant
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import javax.security.auth.callback.*;

import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;

import java.io.IOException;

public class Main {
    /**
     * Create server side Kerberos login context for provided credentials.
     *
     * @param serverPrincipal server principal
     * @param serverPassword  server passsword
     * @return LoginContext server login context
     * @throws LoginException on error
     */
    public static LoginContext serverLogin(final String serverPrincipal,
            final String serverPassword) throws LoginException {
        LoginContext serverLoginContext = new LoginContext("spnego-server",
                new CallbackHandler() {

                    public void handle(Callback[] callbacks)
                            throws IOException,
                            UnsupportedCallbackException {
                        for (Callback callback : callbacks) {
                            if (callback instanceof NameCallback) {
                                final NameCallback nameCallback = (NameCallback) callback;
                                nameCallback.setName(serverPrincipal);
                            } else if (callback instanceof PasswordCallback) {
                                final PasswordCallback passCallback = (PasswordCallback) callback;
                                passCallback.setPassword(serverPassword
                                        .toCharArray());
                            } else {
                                throw new UnsupportedCallbackException(
                                        callback);
                            }
                        }

                    }
                });
        serverLoginContext.login();
        return serverLoginContext;
    }
}

Related

  1. extractName(final X500Principal dname)
  2. extractRDN(String rdn, X500Principal dn)
  3. getDNField(String fieldID, X500Principal principal)
  4. isTGSPrincipal(KerberosPrincipal principal)
  5. isTicketGrantingServerPrincipal(KerberosPrincipal principal)
  6. toGlobusID(X500Principal principal)
  7. toPrincipal(String globusID)
  8. toPrincipal(String globusID)