I have written an application with a secure EJB3 module and a swing GUI app client that is run via java webstart. Everything works great, with authentication for test users allowing me access to the secure EJB. I am using netbeans 6 and glassfish v2 with java 1.5
The problem is that I want to customize the GUI login dialog that pops up. The default GUI login dialog that glassfish provides is not sufficient. I have implemented a callback function and when I run it I get and exception (all can be seen below): "No LoginModules configured".
I want to keep using the LoginModules in glassfish with the jdbcrealm and don't want to write one myself, so I don't know how to proceed. Where do I tell my code to just use the default login module that glassfish was using before?
I have the following callback code:
/**
* JOnAS: Java(TM) Open Application Server
* Copyright (C) 1999-2004 Bull S.A.
* Contact: jonas-team_at_objectweb.org
*
* License: GPL V2
*/
package ca.littleriver.bovinemanagement.appclient;
import java.awt.GridLayout;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class DialogCallbackHandler implements CallbackHandler {
private JTextField loginField = null;
private JTextField passwordField = null;
private boolean cancelled = false;
private String title = "David's Login Dialog";
private String username = "Username ";
private String password = "Password ";
private String loginButton = "Duhh Login !";
private String cancelButton = "Cancel";
private static final int MAX_FIELD_LENGTH = 20;
private int passwordLength = MAX_FIELD_LENGTH;
private int usernameLength = MAX_FIELD_LENGTH;
private char echoChar = '*';
private boolean echoCharOn = false;
private String[] connectOptionNames;
public DialogCallbackHandler() {
super();
int i = 0;
connectOptionNames = new String[2];
connectOptionNames[i] = loginButton;
connectOptionNames[++i] = cancelButton;
}
public DialogCallbackHandler(String title) {
this();
this.title = title;
}
public DialogCallbackHandler(String title, String username, String password) {
this();
this.title = title;
this.username = username;
this.password = password;
}
public DialogCallbackHandler(String title, String username, String password, String loginButton, String cancelButton, int usernameLength, int passwordLength, char echoChar) {
this(title, username, password);
this.echoCharOn = true;
this.echoChar = echoChar;
this.loginButton = loginButton;
this.cancelButton = cancelButton;
this.passwordLength = passwordLength;
this.usernameLength = usernameLength;
this.echoChar = echoChar;
}
private void dialogInit(boolean isEchoOn) {
echoCharOn = (isEchoOn || echoCharOn);
dialogInit();
}
private void dialogInit() {
JLabel userNameLabel = new JLabel(username, JLabel.RIGHT);
loginField = new JTextField("");
JLabel passwordLabel = new JLabel(password, JLabel.RIGHT);
if (!echoCharOn) {
passwordField = new JPasswordField(passwordLength);
((JPasswordField)passwordField).setEchoChar(echoChar);
} else {
passwordField = new JTextField(passwordLength);
}
JPanel connectionPanel = new JPanel(false);
connectionPanel.setLayout(new BoxLayout(connectionPanel, BoxLayout.X_AXIS));
JPanel namePanel = new JPanel(false);
namePanel.setLayout(new GridLayout(0, 1));
namePanel.add(userNameLabel);
namePanel.add(passwordLabel);
JPanel fieldPanel = new JPanel(false);
fieldPanel.setLayout(new GridLayout(0, 1));
fieldPanel.add(loginField);
fieldPanel.add(passwordField);
connectionPanel.add(namePanel);
connectionPanel.add(fieldPanel);
int choice = JOptionPane.showOptionDialog(null, connectionPanel, title, JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null, connectOptionNames, loginField);
if (choice == JOptionPane.OK_OPTION) {
cancelled = false;
} else if (choice == JOptionPane.CANCEL_OPTION) {
cancelled = true;
} else if (choice == JOptionPane.CLOSED_OPTION) {
cancelled = true;
} else {
cancelled = true;
}
if (cancelled) {
loginField.setText("Invalid");
passwordField.setText("Invalid");
}
}
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
if (cancelled) {
return;
}
int i = 0;
boolean found = false;
while ((i < callbacks.length) && !found) {
if (callbacks[i] instanceof PasswordCallback) {
found = true;
dialogInit(((PasswordCallback)callbacks[i]).isEchoOn());
}
i++;
}
if (!found) {
dialogInit();
}
for (i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
((NameCallback)callbacks[i]).setName(loginField.getText());
} else if (callbacks[i] instanceof PasswordCallback) {
((PasswordCallback)callbacks[i]).setPassword((passwordField.getText()).toCharArray());
} else {
throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
}
}
}
}
/**************************************** */
After I see the login window from the above code I get the following exception:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at com.sun.enterprise.appclient.jws.boot.JWSACCMain.run(JWSACCMain.java:218)
at com.sun.enterprise.appclient.jws.boot.JWSACCMain.main(JWSACCMain.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.javaws.Launcher.executeApplication(Launcher.java:1171)
at com.sun.javaws.Launcher.executeMainClass(Launcher.java:1118)
at com.sun.javaws.Launcher.continueLaunch(Launcher.java:961)
at com.sun.javaws.Launcher.handleApplicationDesc(Launcher.java:515)
at com.sun.javaws.Launcher.handleLaunchFile(Launcher.java:218)
at com.sun.javaws.Lau
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:494)
at com.sun.enterprise.appclient.jws.boot.JWSACCMain.run(JWSACCMain.java:218)
at com.sun.enterprise.appclient.jws.boot.JWSACCMain.main(JWSACCMain.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.javaws.Launcher.executeApplication(Launcher.java:1171)
at com.sun.javaws.Launcher.executeMainClass(Launcher.java:1118)
at com.sun.javaws.Launcher.continueLaunch(Launcher.java:961)
at com.sun.javaws.Launcher.handleApplicationDesc(Launcher.java:515)
at com.sun.javaws.Launcher.handleLaunchFile(Launcher.java:218)
at com.sun.javaws.Launcher.run(Launcher.java:165)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:428)
... 17 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.enterprise.util.Utility.invokeApplicationMain(Utility.java:266)
at com.sun.enterprise.appclient.MainWithModuleSupport.<init>(MainWithModuleSupport.java:417)
... 17 more
Caused by: javax.security.auth.login.LoginException: No LoginModules configured for k
at javax.security.auth.login.LoginContext.init(LoginContext.java:256)
at javax.security.auth.login.LoginContext.<init>(LoginContext.java:334)
at ca.littleriver.bovinemanagement.appclient.Main.<init>(Main.java:77)
at ca.littleriver.bovinemanagement.appclient.Main.main(Main.java:186)
... 23 more
[Message sent by forum member 'dwaddy' (dwaddy)]
http://forums.java.net/jive/thread.jspa?messageID=251360