Results 1 to 2 of 2

Thread: HTTP Authentication

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    HTTP Authentication

    Does anyone know how i can implement HTTP Authentication on a site? java.net.url has a method named setDefault(Authenticator a) so i assume that i have to create a custom subclass extending java.net.Authenticator. From what i have read i have to provide implementation for the requestPasswordAuthentication(InetAddress address, int port, String protocol, String prompt, String scheme) method. The docs say that when the url class needs a user name and password, it will ask the class that was passed in to setDefault() for it using the above method. The docs go on to say that the another method that must be overridden is PasswordAuthentication getPasswordAuthentication(). Then we have the PasswordAuthentication class which has it's constructor as PasswordAuthentication(String username, char[] password).

    So how do i piece all of this togther?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Well, here's an example. When authentication is needed, you want to popup a dialog like web browsers do.

    You therefore derive a class from Authenticator.
    Code:
    import java.util.*;
    import java.net.*;
    
    import javax.swing.*;
    
    public class TypicalAuthenticator extends Authenticator
    {
    	HashMap currentLogins = new HashMap();
    
    	/** Gives the authentication info to the URL connection. */
    	protected PasswordAuthentication getPasswordAuthentication() {
    		Object o = currentLogins.get(getRequestingSite());
    		if(o == null) {
    			o = askForPassword();
    			currentLogins.add(getRequestingSite(), o);
    		}
    		return (PasswordAuthentication)o;
    	}
    
    	/** Retrieves info from the user. */
    	private PasswordAuthentication askForPassword() {
    		// Show some nice dialog with two inputs here.
    		// I'll just use two JOptionPane:input dialogs.
    		String user = JOptionPane.showInputDialog("Please enter a username for:\n" +
    			getRequestingPrompt());
    		String pass = JOptionPane.showInputDialog("Please enter the password:");
    		return new PasswordAuthentication(user, pass.toCharArray());
    	}
    }
    The rest is easy.

    The example should be fully functional, but it doesn't offer escape routes, password hiding or any error checking. And it is untested.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width