JAAS and Win2K Active Directory - can't login
843811Feb 22 2002 — edited May 12 2003Hi.
I try to write a login piece of code tht checks against a windows 200 Server Active Directory.
I started from the tutorial code (see my code below). For simplicity, I have hardcoded the username and password. I specified the kerberos login module in my jaas.config file.
I always get the following message from the server:
Pre-authentication information was invalid (24)
After 3 attempts, my user account gets locked-out, which means that the login module communicates successfully with Win2K Server.
Can anyone help?
Thanks.
public class LoginApp
{
public LoginApp()
{
LoginContext lc = null;
try
{
lc = new LoginContext("kerberos", new MyCallbackHandler());
}
catch (LoginException le)
{
System.err.println("Cannot create LoginContext. " + le.getMessage());
System.exit(-1);
}
catch (SecurityException se)
{
System.err.println("Cannot create LoginContext. " + se.getMessage());
System.exit(-1);
}
// the user has 3 attempts to authenticate successfully
int i;
for (i = 0; i < 1; i++) {
try {
// attempt authentication
lc.login();
System.out.println("SUCCESS!!!");
// if we return with no exception,
// authentication succeeded
break;
} catch (LoginException le) {
System.err.println("Authentication failed:");
System.err.println(" " + le.getMessage());
try {
Thread.currentThread().sleep(3000);
} catch (Exception e) {
// ignore
}
}
}
// did they fail three times?
if (i == 3) {
System.out.println("Sorry");
System.exit(-1);
}
}
/**
*
*/
public static void main(String[] args)
{
LoginApp app = new LoginApp();
}
/**
* The application implements the CallbackHandler.
*
* <p> This application is text-based. Therefore it displays information
* to the user using the OutputStreams System.out and System.err,
* and gathers input from the user using the InputStream System.in.
*/
class MyCallbackHandler implements CallbackHandler {
/**
* Invoke an array of Callbacks.
*
* <p>
*
* @param callbacks an array of <code>Callback</code> objects which contain
* the information requested by an underlying security
* service to be retrieved or displayed.
*
* @exception java.io.IOException if an input or output error occurs. <p>
*
* @exception UnsupportedCallbackException if the implementation of this
* method does not support one or more of the Callbacks
* specified in the <code>callbacks</code> parameter.
*/
public void handle(Callback[] callbacks)
throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof TextOutputCallback) {
System.out.println("-1-");
// display the message according to the specified type
TextOutputCallback toc = (TextOutputCallback)callbacks;
switch (toc.getMessageType()) {
case TextOutputCallback.INFORMATION:
System.out.println(toc.getMessage());
break;
case TextOutputCallback.ERROR:
System.out.println("ERROR: " + toc.getMessage());
break;
case TextOutputCallback.WARNING:
System.out.println("WARNING: " + toc.getMessage());
break;
default:
throw new IOException("Unsupported message type: " +
toc.getMessageType());
}
} else if (callbacks[i] instanceof NameCallback) {
// prompt the user for a username
NameCallback nc = (NameCallback)callbacks[i];
System.err.print(nc.getPrompt());
System.err.flush();
// nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine());
nc.setName("username");
} else if (callbacks[i] instanceof PasswordCallback) {
// prompt the user for sensitive information
PasswordCallback pc = (PasswordCallback)callbacks[i];
System.err.print(pc.getPrompt());
System.err.flush();
// pc.setPassword(readPassword(System.in));
pc.setPassword("password".toCharArray());
} else {
throw new UnsupportedCallbackException
(callbacks[i], "Unrecognized Callback");
}
}
}
}
}