|
Application Techniques |
How to code a support class for external Java clients (outside of the SilverStream development environment) that handles logins when the SilverStream server calls back for user authentication.
|
About this technique |
Details |
|---|---|
See the chapter on writing external Java clients in the Programmer's Guide |
The login handler technique involves coding a class that obtains a username and password, then returns them to the SilverStream server. The design of this class depends on how you go about getting those values:
To prompt the user via standard output/input, see Coding a basic login handler
To prompt the user via a dialog, see Coding a graphical login handler
If you want to obtain the username and password in some other way (such as from command-line arguments), you can use one of the provided classes as a starting point and modify it.
This implementation of the login handler prompts for a username and password by using standard output/input.
package extjtech;
import java.io.*;
import com.sssw.rt.util.*;
// The LoginHandler class shows how you can code a
// SilverStream login handler for use in external Java
// clients. You'll supply an instance of this class in the
// AgRuntime.init() method when preparing to connect your
// client to a SilverStream server. This enables the server
// to call back to that LoginHandler instance when it
// requires user authentication.
//
// LoginHandler implements the SilverStream interface
// AgiUserLogin, which has one method: prompt(). You must
// code that method to prompt the user for name and password,
// then return those values to the server (in an instance of
// the SilverStream class AgoUserLoginInfo).
//
// This example prompts the user by writing prompt text to
// standard output and reading data entry from standard
// input. Alternatively, you could display a dialog (as
// shown in class LoginHandlerDlg) or read command-line args
// to obtain the name and password.
public class LoginHandler implements AgiUserLogin {
public AgoUserLoginInfo prompt(String realm,
String username,
boolean forceDialog) {
BufferedReader in = null;
String user = null;
String password = null;
try {
// Prepare to read data entered from standard input.
in = new BufferedReader(new InputStreamReader(System.in));
// Ask the user to log in to this security realm.
System.out.println("Please log in for " + realm);
// Obtain the username.
System.out.print("username: ");
System.out.flush();
user = in.readLine();
// If a username was entered, obtain the password.
// Note: A limitation of this approach is that the
// password displays on the user's screen.
if (user != null && user.length() != 0) {
System.out.print("password: ");
System.out.flush();
password = in.readLine();
}
}
catch (IOException ex) {
System.out.println("I/O error during login prompt");
ex.printStackTrace();
}
// Return the username and password to the server for
// authentication.
return new AgoUserLoginInfo(user, password);
}
}
When this login handler class runs, it prompts the user as follows:
To learn more about using this class in an application, see any of these related Application Techniques:
This implementation of the login handler prompts for a username and password by displaying a dialog.
package extjtech;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.sssw.rt.util.*;
// The LoginHandlerDlg class shows how you can code a
// SilverStream login handler for use in external Java
// clients. You'll supply an instance of this class in the
// AgRuntime.init() method when preparing to connect your
// client to a SilverStream server. This enables the server
// to call back to that LoginHandlerDlg instance when it
// requires user authentication.
//
// LoginHandlerDlg implements the SilverStream interface
// AgiUserLogin, which has one method: prompt(). You must
// code that method to prompt the user for name and password,
// then return those values to the server (in an instance of
// the SilverStream class AgoUserLoginInfo).
//
// This example prompts the user by displaying a dialog.
// Alternatively, you could read standard input (as shown in
// class LoginHandler) or command-line args to obtain the
// name and password.
public class LoginHandlerDlg implements AgiUserLogin {
// Instance variables for the LoginHandlerDlg class.
String user;
String password;
JFrame frame;
JDialog dialog;
JTextField namefield;
JPasswordField passfield;
public AgoUserLoginInfo prompt(String realm,
String username,
boolean forceDialog) {
// Start setting up the dialog to display. Include
// the name of the security realm (supplied by the
// server) in its title.
frame = new JFrame();
dialog = new JDialog(frame, "Login for " + realm, true);
// Set up the username field. If the server supplies a
// username value from a prior login try, display it.
JLabel namelabel = new JLabel("Username: ");
namefield = new JTextField(10);
namefield.setText(username);
// Set up the password field.
JLabel passlabel = new JLabel("Password: ");
passfield = new JPasswordField(10);
passfield.setEchoChar('#');
// Set up the OK button, including an event handler
// that executes when a user clicks the button. This
// event handler obtains the username and password
// entered in the fields, then closes the dialog.
JButton button = new JButton("OK");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
user = namefield.getText();
password = new String(passfield.getPassword());
frame.dispose();
}
});
// Lay out the labels in a panel.
JPanel labelpane = new JPanel();
labelpane.setLayout(new GridLayout(0, 1));
labelpane.add(namelabel);
labelpane.add(passlabel);
// Lay out the fields in a panel.
JPanel fieldpane = new JPanel();
fieldpane.setLayout(new GridLayout(0, 1));
fieldpane.add(namefield);
fieldpane.add(passfield);
// Now put everything in another panel: labels on
// left, fields on right, button on bottom.
JPanel contentpane = new JPanel();
contentpane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
contentpane.setLayout(new BorderLayout());
contentpane.add(labelpane, BorderLayout.CENTER);
contentpane.add(fieldpane, BorderLayout.EAST);
contentpane.add(button, BorderLayout.SOUTH);
// Finish setting up the dialog, then show it.
dialog.setContentPane(contentpane);
dialog.pack();
dialog.setLocation(100, 100);
dialog.setVisible(true);
// Return the username and password to the server for
// authentication.
return new AgoUserLoginInfo(user, password);
}
}
When this login handler class runs, it prompts the user as follows:
To learn more about using this class in an application, see the related technique:
Providing Data to JTables in External Java Clients.
|
Application Techniques |
Copyright © 2001, SilverStream Software, Inc. All rights reserved.