这是可能的。例如,您可以将每个配置放在单独的文件中,然后在每次尝试开始时将 Java 路径传递给 krb5.ini 和 login.conf 文件:
boolean loginSuccess = false;
try
{
System.setProperty("java.security.krb5.conf", "C:\kerb\conf1\krb5.ini");
System.setProperty("java.security.auth.login.config", "C:\kerb\conf1\login.conf");
// in login.conf you can have defined path to keytab for this configuration
LoginContext lc = new LoginContext(...);
//Try Realm 1
lc.login();
loginSuccess = true;
}
catch(LoginException le)
{
try
{
System.setProperty("java.security.krb5.conf", "C:\kerb\conf2\krb5.ini");
System.setProperty("java.security.auth.login.config", "C:\kerb\conf2\login.conf");
// in login.conf you can have defined path to keytab for this configuration
LoginContext lc2 = new LoginContext(...);
//Try Realm 2
lc2.login();
loginSuccess = true;
}
catch(LoginException le)
{
//...
}
}
return loginSuccess;
此处描述了这两个系统属性:http:
//docs.oracle.com/javase/7/docs/technotes/guides/security/jgss/tutorials/LoginConfigFile.html和
http://docs.oracle.com/javase/ 7/docs/technotes/guides/security/jgss/tutorials/KerberosReq.html
另一种可能性可能是没有文件的配置。有一个 LoginContext 构造函数,它接受 CallbackHandler(这里你传递用户名和密码)和 Configuration(这里你传递你在 login.conf 中的参数)。kdc 和 realm 可以在系统属性 java.security.krb5.realm 和 java.security.krb5.kdc 中传递
http://docs.oracle.com/javase/8/docs/api/javax/security/auth/login/LoginContext.html#LoginContext-java.lang.String-javax.security.auth.Subject-javax.security。 auth.callback.CallbackHandler-javax.security.auth.login.Configuration-