Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Password Authentication

Status
Not open for further replies.

LoudHoward

Technical User
Joined
May 10, 2001
Messages
2
Location
AU
Hi,
I am doing a Java assignment, and as part of the spec we have to add simple authentication to access certain functions such as insert update and delete.

I am using Swing and there is a button to access the admin functions. How apply simple username/password authentication (i.e dialog pops up, uname/pword entered and then checked)

Cheers
 
Hi LoudHoward,
At fist I have to mention that I'm not a GUI expert so there might be much better ways to do this.

I used JDialog and java.awt.TextField for getting password, because I don't know if there is a method to set echoChar in JTextField...

But here is the code to implement Dialog that asks for username and password:
_________________________________________________________

import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Passwd extends JFrame
{
public static void main(String[] argv)
{
new Passwd();
}

public Passwd()
{
super("TestFrame");

_dialog = new PWDialog(this, "Password check");

JButton btn = new JButton("pushMe");

btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(_dialog.authenticate())
System.out.println("Permission granted");
else
System.out.println("Permission denied!");
}
});


Container c = this.getContentPane();

c.add(btn);

this.setSize(100,100);
this.pack();
this.show();
}


class PWDialog extends JDialog
{
public PWDialog(JFrame parent, String title)
{
super(parent, title, true); // Use modal dialog

GridLayout grid = new GridLayout(3,2,0,3); // Define layout

Container c = this.getContentPane();
c.setLayout(grid);

Label lblUser = new Label("UserName");
c.add(lblUser);

_txtUserName = new TextField();
c.add(_txtUserName);

Label lblPasswd = new Label("Password");
c.add(lblPasswd);

_txtPasswd = new TextField();
_txtPasswd.setEchoChar('*');
c.add(_txtPasswd);

_btnOk = new JButton("OK");
_btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
check(); // Check username & password
}
});
c.add(_btnOk);

_btnCancel = new JButton("Cancel");
_btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
check();
}
});
c.add(_btnCancel);

this.setSize(200,100);
this.setResizable(false);
}

private void check()
{
if(_txtUserName.getText().equals("foo") && _txtPasswd.getText().equals("bar"))
{
_passed = true; // permission granted
}
else
{
_passed = false;
}

_txtPasswd.setText("");
_txtUserName.setText("");
this.hide(); // Hide dialog (returns _passed attribute at authenticate() method)
}

public boolean authenticate()
{
this.show();
return _passed;
}

private TextField _txtUserName;
private TextField _txtPasswd;
private JButton _btnOk;
private JButton _btnCancel;

private boolean _passed = false;
}

private PWDialog _dialog;
}
_________________________________________________________

Accepted Username and Password are hardcoded here, but
you probably want to get them from file or database etc.

I you have further questions, please ask!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top