JaasAuthentication
package com.ack.security.jaas;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
/**
* This simple jaas application attempts to authenticate a user
* and reports whether or not the authentication was successful.
*
*
To run this you need a couple of things:
*
*
*/
public class JaasAuthentication {
public static void main(String[] args) {
// Obtain a LoginContext, needed for authentication. Tell it
// to use the LoginModule implementation specified by the
// entry named "xxx" in the JAAS login configuration
// file and to also use the specified CallbackHandler.
LoginContext lc = null;
try {
lc = new LoginContext("xxx", new JaasLoginCallbackHandler());
}
catch( LoginException le ) {
System.err.println("Cannot create LoginContext. " + le.getMessage());
}
catch( SecurityException se ) {
System.err.println("Cannot create LoginContext. " + se.getMessage());
}
finally {
if( lc == null ) {
System.exit(-1);
}
}
// attempt authentication
try {
lc.login();
System.out.println("Authentication succeeded!");
}
catch( LoginException le ) {
System.err.println("Authentication failed:\n" + " " + le.getMessage());
}
}
}