java使用Kerberos认证

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.security.auth.Subject;

import com.sun.security.auth.module.Krb5LoginModule;

/**
 * This is simple Java program that tests ability to authenticate 
 * with Kerberos using the JDK implementation.
 * 
 * The program uses no libraries but JDK itself.
 */
public class Krb {
     

  private void loginImpl() throws Exception {
     
    System.out.println("NB: system property to specify the krb5 config: [java.security.krb5.conf]");
    //System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");

    System.out.println(System.getProperty("java.version"));

    System.setProperty("sun.security.krb5.debug", "true");

    final Subject subject = new Subject();
    final Krb5LoginModule krb5LoginModule = new Krb5LoginModule();
    final Map<String,String> optionMap = new HashMap<String,String>();
    optionMap.put("keyTab", "/etc/krb5.keytab");
    optionMap.put("principal", "foo"); 
    optionMap.put("doNotPrompt", "true");
    optionMap.put("refreshKrb5Config", "true");
    optionMap.put("useTicketCache", "true");
    optionMap.put("renewTGT", "true");
    optionMap.put("useKeyTab", "true")
    optionMap.put("storeKey", "true");
    optionMap.put("isInitiator", "true");
    optionMap.put("debug", "true"); // switch on debug of the Java implementation
    
    krb5LoginModule.initialize(subject, null, new HashMap<String,String>(), optionMap);

    boolean loginOk = krb5LoginModule.login();
    System.out.println("======= login:  " + loginOk);

    boolean commitOk = krb5LoginModule.commit();
    System.out.println("======= commit: " + commitOk);

    System.out.println("======= Subject: " + subject);
  }

  public static void main(String[] args) throws Exception {
     
    System.out.println("A property file with the login context can be specified as the 1st and the only paramater.");
    final Krb krb = new Krb();
    krb.loginImpl();
  }
}

你可能感兴趣的:(java,kerberos,java)