NetBeans Platform Login Tutorial
NetBeans Platform Login Tutorial
This tutorial demonstrates how to create a login form for a NetBeans Platform application with very simple user and password management. The login will be implemented with the
DialogDisplayer class of the NetBeans APIs.
The dialog that you create in this tutorial will be visible whenever the user starts the application. The passwords and usernames will be stored in a preferences file, which is not
very safe. Therefore, this tutorial will provide a solution to encrypt the passwords with "Message-Digest Algorithm 5" (MD5). MD5 is a cryptographic hash-function invented by
Roland L. Rivest. To use MD5 in our application, the [Link] package will help us.
Contents
o Other Possibilities
o [Link]
o [Link]
o [Link]
Next Steps
Versioning
For more information on working with modules, see the NetBeans Development Project home on the NetBeans website. If you have questions, visit the NetBeans Developer FAQ or
use the feedback link at the bottom of this page.
Java Standard Development Kit (JDK™) version 1.4.2 (download) or 5.0 (download) or later
2. In the next panel you have to give the suite a proper name, such as LoginSuite, and set the location to any directory on your computer. Click Finish.
3. Now the IDE has created an empty suite for you. You can even test it when you right-click the module suite name and choose run. There will start a whole new IDE. But
we do not need a complete IDE. So if you like, you can make the start of your application faster by following these few optional steps:
4. Right-click the module suite name. Choose 'Properties'. Select 'Libraries'. Now you see on the right side a list of 'Platfom Modules'. Deselect all except of 'platform7'.
Click OK.
5. If you run the application again there will only be a window with the essential buttons and menus. It should look like this:
Creating A Simple User And Password Management
To have as little work as possible, we will use some very interesting features of NetBeans IDE to create our simple user management. This management will only be able to store
one username and one password in a preferences file.
Create Option Panel For The User Management
In this section we will show you how to create an option panel to handle your username and password.
1. At first we create a new module by right-clicking on the Modules folder in our suite and choosing Add New....
2. Now a wizard for adding a new module appears. You have to enter a name for your module, for example 'UserManagement'. Click Next.
3. Change now the Code Base name, so that it fits into your application. Click Finish.
After that the NetBeans IDE has created a new module for you. So, now we have to create a so called options panel to easily set our password and username.
1. Right-click on the Source Packages of your new module and select New > Other... .
2. A wizard appears and you should select the category 'Module Development' and in the list of filetypes you should select 'Options Panel'. Click Next.
3. Here we can choose whether we want a whole new panel category or a new panel in the 'Miscellaneous' category. In our case we will use the 'Miscellaneous' one. To
complete this task enter a name and a tool tip text. Click Next.
4. On this panel you can now choose a class name prefix. I recommend to accept the IDE suggestion. Click Finish.
Next there are some files opened by the IDE. For now we only need the <ClassNamePrefix>[Link]. Go to this file and go into the Design-View and follow the instructions
below.
1. Use the Palette and add two labels and two textfields.
2. Then rename the top label to 'Username' and the bottom one to 'Password'. Delete the texts of the textfields and rename the variables to 'user' and 'pass'.
3. Now go to the Source-View and look for two methods called load and store. These two methods are used to initialize the textfields and to store changes.
5. void store() {
6. [Link]([Link]).put("user", [Link]());
7. [Link]([Link]).put("pass", [Link]());
}
With this code a new NetBeans Preferences file is created and the information of the textfields is stored into it with the written keys. Then add imports for
[Link].
8. Now change the source code for 'load' in the following way:
9. void load() {
10. [Link]([Link]([Link]).get("user", ""));
11. [Link]([Link]([Link]).get("pass", ""));
}
In that part of the code the NetBeans Preferences are used to fill the textfields.
When you start your application you will find your new options panel in 'Tools > Options > Miscellaneous'. You can also enter a password and an username now. When your restart
your application you will see that these changes are persistant.
Now our mini user management is complete. We have one user with one password.
Use MD5 To Encrypt Password
Here we will show you the encryption of your password with the MD5 algorithm.
A very good possibility to make your system safer is to encrypt your password with an algorithm called MD5. The 'Message-Digest Algorithm 5' is a cryptographic hash-function
that was invented by Roland L. Rivest. To use MD5 in our application we can use some features of Java. The [Link] package will help us.
But we have a problem with our created options panel. A MD5-Hash cannot be decrypted in a string. It is only possible to compare two MD5 values. So if you want to use this, you
have to do some changes on your code in the 'store' method.
void store() {
[Link]([Link]).put("user", [Link]());
if([Link]([Link]).get("pass", "").equals([Link]())){
//do nothing with password
} else {
try {
String passwordMD5 = null;
MessageDigest md5 = [Link]("MD5");
byte[] tmp = [Link]().getBytes();
[Link](tmp);
passwordMD5 = byteArrToString([Link]());
[Link]([Link]).put("pass", passwordMD5);
} catch (NoSuchAlgorithmException ex) {
[Link](ex);
}
}
}
Other Possibilities
In this section we will show you the encryption of your password with the SHA-1 algorithm.
Another possibility is to use the SHA-1 instead of the MD5. SHA-1 (secure hash algorithm) was decrypted in 2006 but for our purpose it is safe enough. You just have to do some
little changes on the MD5 code shown above.
void store() {
[Link]([Link]).put("user", [Link]());
if([Link]([Link]).get("pass", "").equals([Link]())){
//do nothing with password
} else {
try {
String passwordSHA = null;
MessageDigest sha = [Link]("SHA-1");
byte[] tmp = [Link]().getBytes();
[Link](tmp);
passwordMD5 = byteArrToString([Link]());
[Link]([Link]).put("pass", passwordMD5);
} catch (NoSuchAlgorithmException ex) {
[Link](ex);
}
}
}
The only real change is to use the java MessageDigist to create an instance of a SHA-1 instead of a MD5.
To secure the application a little bit you should save your usernames and passwords somewhere else than in a preferences file. Maybe a database or a file on a safe server will be a
better way.
2. Give the new module a name like 'Login' and click Next.
4. When the new module is located in the Projects window, right-click on the 'Source Packages' and choose 'New' > 'Other...'.
5. In the appearing wizard select 'Module Development' as a category and then 'Module Installer' as file-type. Click Next.
6. Now you see again which files are changed or created. Click Finish.
import [Link];
/**
* Manages a module's lifecycle. Remember that an installer is optional and
* often not needed at all.
*/
public class Installer extends ModuleInstall {
@Override
public void restored() {
// By default, do nothing.
// Put your startup code here.
}
}
The code that we will write in the restored() method will be called before the application starts. So we will use this to show our login form. But this form has to be created first.
1. Right-click the 'Source Packages' and choose 'New' > 'Other...'.
2. Select 'Swing GUI Forms' as category and 'JPanel Form' as filetype. Click Next.
3. Give the form a name like 'LoginFrame' and select the correct package. Click Finish.
4. If it has not been opened yet, open the LoginFrame and go to the Design-View.
6. Name the labels 'Username' and 'Password', delete the text in the fields and rename the variables of the fields to 'user' and 'pass'.
Now we can call our LoginForm in the [Link]. Go to the [Link] and add a private field LoginForm form = new LoginForm().
Now we have to call something in the restored()method. For our login dialog we will use the NotifyDescriptor from the NetBeans API.
1. Add the following code to the restored() method in the [Link]:
7. Now you will see that there is a 'Dialogs API'. Select it and click OK.
When you start the application you will see that there is a dialog with an 'OK' button in it. This will be the dialog where we will call our LoginForm.
To create a better structure we will create a new method that will be called in the restored() method.
We do not want to have a simple dialog, but a diaolg with an 'OK' and a 'Cancel' button. For this purpose we will use the Confirmation method instead of the Message method of the
NotifyDescriptor. The changed class Installer should look like this:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package [Link];
import [Link];
import [Link];
import [Link];
/**
* Manages a module's lifecycle. Remember that an installer is optional and
* often not needed at all.
*/
public class Installer extends ModuleInstall {
private LoginForm form = new LoginForm();
@Override
public void restored() {
createLoginDialog();
For a login we only want to have an 'OK' and a 'Cancel' button. So we have to create them. This time we cannot use the graphical designer, so we have to code it ourselves which is
not as difficult as it maybe sounds. You only have to change our createLoginDialog() method.
private void createLoginDialog(){
JButton ok = new JButton();
[Link]("OK");
JButton cancel = new JButton();
[Link]("Cancel");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//close whole application
}
});
[Link](new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//authenicate username and password
}
});
8. If you find it, select our 'UserManagement' module and click OK.
9. Click OK.
10. Open the [Link] and alter the class definition by adding 'public' before the 'final' keyword.
[Link]
In the [Link] we create a panel in the options dialog of the NetBeans Platform to manage our username and password. Most of the code is created by the
NetBeans IDE.
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
UsermanagementPanel(UsermanagementOptionsPanelController controller) {
[Link] = controller;
initComponents();
// TODO listen to changes in form fields and call [Link]()
}
[Link](jLabel1, "Username");
[Link](jLabel2, "Password");
void load() {
[Link]([Link]([Link]).get("user", ""));
[Link]([Link]([Link]).get("pass", ""));
}
void store() {
[Link]([Link]).put("user", [Link]());
if([Link]([Link]).get("pass", "").equals([Link]())){
//do nothing with password
} else {
try {
String passwordMD5 = null;
MessageDigest md5 = [Link]("MD5");
byte[] tmp = [Link]().getBytes();
[Link](tmp);
passwordMD5 = byteArrToString([Link]());
[Link]([Link]).put("pass", passwordMD5);
} catch (NoSuchAlgorithmException ex) {
[Link](ex);
}
}
}
boolean valid() {
// TODO check whether form is consistent and complete
return true;
}
}
[Link]
The [Link] creates a dialog in which the password and username can be entered to start your application. This class is also quickly created when you use the tools of the
NetBeans IDE.
/*
* [Link]
*
* Created on 03. Dezember 2007, 21:39
*/
package [Link];
/**
*
* @author Christof and Sabine
*/
public class LoginForm extends [Link] {
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
* Manages a module's lifecycle. Remember that an installer is optional and
* often not needed at all.
*/
public class Installer extends ModuleInstall {
@Override
public void restored() {
createLoginDialog();
}
[Link](new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
authenticate();
}
});
[Link](new PropertyChangeListener(){
public void propertyChange(PropertyChangeEvent evt){
if(NotifyDescriptor.CLOSED_OPTION.equals([Link]())){
exit();
}
}
});
}