Simplify enterprise Java authentication with single-sign on

Listing 5. GSSClientApplet

A GSS applet client

Return to article.

Listing 5. GSSClientApplet
/****
    GSSClientApplet.java
****/

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

import org.ietf.jgss.*;

public class GSSClientApplet extends Applet {

    //Instance of GSSClient bean and GSS context.
    GSSClient gssClient = null;
    GSSContext context = null;

    //Labels for user input fields.
    Label lblUserName = new Label ("EMarketplace ID :");
    Label lblPassword = new Label ("Password :");

    //Text input fileds for user name and password.
    TextField tfUserName = new TextField (12);
    TextField tfPassword = new TextField (12);

    //Buttons representing emarketplace partners.
    Button buttonPartner1 = new Button("  Login to Partner1  ");
    Button buttonPartner2 = new Button("  Login to Partner2  ");
    Button buttonPartner3 = new Button("  Login to Partner3  ");

    Color bgColor = new Color (204,204,255);
	
    //TextArea to show login progress.
    TextArea taResponse = null;

    //GSS related parameters.
    String remotePeer = null;
    String kerberosRealm = null;
    String kdcAddress = null;
    String addressOfRemotePeer = null;
    int portOfRemotePeer;
    String confName = null;
    String confFile = null;

    
    // Intializes applet with appropiate layout and listners.
    public void init() 
    {
        setLayout(new FlowLayout(FlowLayout.CENTER));
        add(lblUserName);
        add(tfUserName);
        add(lblPassword);
        add(tfPassword);

        buttonPartner1.setBackground(bgColor);
        buttonPartner2.setBackground(bgColor);		
        buttonPartner3.setBackground(bgColor);
    
        kerberosRealm = "EMARKET.LOCAL";
        kdcAddress = "pak.emarket.local:88";
        addressOfRemotePeer = "pak";

        confFile = "C:/login.conf";
        confName = "GSSClient";
		
        add(buttonPartner1);
        buttonPartner1.addActionListener ( new ActionListener() {
     	      public void actionPerformed(ActionEvent evt)
            {
                remotePeer = "partner1";
                portOfRemotePeer = 1080;
                login();
            }
        }//ActionListener
        );

        add(buttonPartner2);
        buttonPartner2.addActionListener ( new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {
                remotePeer = "partner2";
                portOfRemotePeer = 1082;
                login();				
            }//action performed
        }//ActionListener
        );

        add(buttonPartner3);
        buttonPartner3.addActionListener ( new ActionListener() {
            public void actionPerformed(ActionEvent evt)
            {
                remotePeer = "partner3";
                portOfRemotePeer = 1084;
                login();
            }//action performed
        }//ActionListener
        );


        taResponse = new TextArea("[Output Window]....\n\r",12,58);
        taResponse.setBackground(Color.white);
        add (taResponse);

    }//init()

  
    private void login()
    {
	    try {
	    
	        if (tfUserName.getText().equals("") @amp;@amp; 
                  tfPassword.getText().equals(""))
	            taResponse.append("Please use your E-Commerce site Id to login..\n\r");
	        else  
	        {
	            gssClient = new GSSClient (
                              tfUserName.getText()+"@"+kerberosRealm, 
                              tfPassword.getText(), 
                              remotePeer, 
                              addressOfRemotePeer, 
                              portOfRemotePeer,
                              kerberosRealm,
                              kdcAddress,
                              confFile,
                              confName);

                  taResponse.append(tfUserName.getText()+" being logged in..\n\r");
	            context = gssClient.login();
	            if (context!=null)
                  {
                      //Checking confidentiality status of context.
                      if (context.getConfState())
                      { 
                          String message = new String ("A sample message from client");
                          taResponse.append("You are successfully logged in.. \n\r");
                          taResponse.append("Sending ["+message+"] to server \n\r");
	                    String response = gssClient.sendMessage(context, message);
                          taResponse.append("Server response ... "+response+"\n\r");
	                }
	                else
                          taResponse.append("Context confidentiality failed...\n\r");
	        	         
                      //Closing Login and GSS contexts.
	                try {
                          gssClient.getLoginContext().logout();
                          context.dispose();
	                } catch (Exception e) {
                          e.printStackTrace();
	                }//catch
	            }
	            else
                      taResponse.append("Context establishment failed...\n\r");
	    
	        }//else

	    }//try	
	    catch (Exception e) {
              taResponse.append("Exception..."+e.getMessage()+"\n\r");
          }//catch
	
    }//login

}//GSSClientApplet

Return to article.