java.net
Class Authenticator

java.lang.Object
  extended byjava.net.Authenticator

public abstract class Authenticator
extends Object

The class Authenticator represents an object that knows how to obtain authentication for a network connection. Usually, it will do this by prompting the user for information.

Applications use this class by creating a subclass, and registering an instance of that subclass with the system with setDefault(). When authentication is required, the system will invoke a method on the subclass (like getPasswordAuthentication). The subclass's method can query about the authentication being requested with a number of inherited methods (getRequestingXXX()), and form an appropriate message for the user.

All methods that request authentication have a default implementation that fails.

Since:
1.2
Author:
Bill Foote
See Also:
setDefault(java.net.Authenticator), getPasswordAuthentication()

Field Summary
private  String requestingHost
           
private  int requestingPort
           
private  String requestingPrompt
           
private  String requestingProtocol
           
private  String requestingScheme
           
private  InetAddress requestingSite
           
private static Authenticator theAuthenticator
           
 
Constructor Summary
Authenticator()
           
 
Method Summary
protected  PasswordAuthentication getPasswordAuthentication()
          Called when password authorization is needed.
protected  String getRequestingHost()
          Gets the hostname of the site or proxy requesting authentication, or null if not available.
protected  int getRequestingPort()
          Gets the port number for the requested connection.
protected  String getRequestingPrompt()
          Gets the prompt string given by the requestor.
protected  String getRequestingProtocol()
          Give the protocol that's requesting the connection.
protected  String getRequestingScheme()
          Gets the scheme of the requestor (the HTTP scheme for an HTTP firewall, for example).
protected  InetAddress getRequestingSite()
          Gets the InetAddress of the site requesting authorization, or null if not available.
static PasswordAuthentication requestPasswordAuthentication(InetAddress addr, int port, String protocol, String prompt, String scheme)
          Ask the authenticator that has been registered with the system for a password.
static PasswordAuthentication requestPasswordAuthentication(String host, InetAddress addr, int port, String protocol, String prompt, String scheme)
          Ask the authenticator that has been registered with the system for a password.
private  void reset()
           
static void setDefault(Authenticator a)
          Sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authentication.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

theAuthenticator

private static Authenticator theAuthenticator

requestingHost

private String requestingHost

requestingSite

private InetAddress requestingSite

requestingPort

private int requestingPort

requestingProtocol

private String requestingProtocol

requestingPrompt

private String requestingPrompt

requestingScheme

private String requestingScheme
Constructor Detail

Authenticator

public Authenticator()
Method Detail

reset

private void reset()

setDefault

public static void setDefault(Authenticator a)
Sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authentication.

First, if there is a security manager, its checkPermission method is called with a NetPermission("setDefaultAuthenticator") permission. This may result in a java.lang.SecurityException.

Parameters:
a - The authenticator to be set. If a is null then any previously set authenticator is removed.
Throws:
SecurityException - if a security manager exists and its checkPermission method doesn't allow setting the default authenticator.
See Also:
SecurityManager.checkPermission(java.security.Permission), NetPermission

requestPasswordAuthentication

public static PasswordAuthentication requestPasswordAuthentication(InetAddress addr,
                                                                   int port,
                                                                   String protocol,
                                                                   String prompt,
                                                                   String scheme)
Ask the authenticator that has been registered with the system for a password.

First, if there is a security manager, its checkPermission method is called with a NetPermission("requestPasswordAuthentication") permission. This may result in a java.lang.SecurityException.

Parameters:
addr - The InetAddress of the site requesting authorization, or null if not known.
port - the port for the requested connection
protocol - The protocol that's requesting the connection (getRequestingProtocol())
prompt - A prompt string for the user
scheme - The authentication scheme
Returns:
The username/password, or null if one can't be gotten.
Throws:
SecurityException - if a security manager exists and its checkPermission method doesn't allow the password authentication request.
See Also:
SecurityManager.checkPermission(java.security.Permission), NetPermission

requestPasswordAuthentication

public static PasswordAuthentication requestPasswordAuthentication(String host,
                                                                   InetAddress addr,
                                                                   int port,
                                                                   String protocol,
                                                                   String prompt,
                                                                   String scheme)
Ask the authenticator that has been registered with the system for a password. This is the preferred method for requesting a password because the hostname can be provided in cases where the InetAddress is not available.

First, if there is a security manager, its checkPermission method is called with a NetPermission("requestPasswordAuthentication") permission. This may result in a java.lang.SecurityException.

Parameters:
host - The hostname of the site requesting authentication.
addr - The InetAddress of the site requesting authentication, or null if not known.
port - the port for the requested connection.
protocol - The protocol that's requesting the connection (getRequestingProtocol())
prompt - A prompt string for the user which identifies the authentication realm.
scheme - The authentication scheme
Returns:
The username/password, or null if one can't be gotten.
Throws:
SecurityException - if a security manager exists and its checkPermission method doesn't allow the password authentication request.
Since:
1.4
See Also:
SecurityManager.checkPermission(java.security.Permission), NetPermission

getRequestingHost

protected final String getRequestingHost()
Gets the hostname of the site or proxy requesting authentication, or null if not available.

Returns:
the hostname of the connection requiring authentication, or null if it's not available.
Since:
1.4

getRequestingSite

protected final InetAddress getRequestingSite()
Gets the InetAddress of the site requesting authorization, or null if not available.

Returns:
the InetAddress of the site requesting authorization, or null if it's not available.

getRequestingPort

protected final int getRequestingPort()
Gets the port number for the requested connection.

Returns:
an int indicating the port for the requested connection.

getRequestingProtocol

protected final String getRequestingProtocol()
Give the protocol that's requesting the connection. Often this will be based on a URL, but in a future SDK it could be, for example, "SOCKS" for a password-protected SOCKS5 firewall.

Returns:
the protcol, optionally followed by "/version", where version is a version number.
See Also:
URL.getProtocol()

getRequestingPrompt

protected final String getRequestingPrompt()
Gets the prompt string given by the requestor.

Returns:
the prompt string given by the requestor (realm for http requests)

getRequestingScheme

protected final String getRequestingScheme()
Gets the scheme of the requestor (the HTTP scheme for an HTTP firewall, for example).

Returns:
the scheme of the requestor

getPasswordAuthentication

protected PasswordAuthentication getPasswordAuthentication()
Called when password authorization is needed. Subclasses should override the default implementation, which returns null.

Returns:
The PasswordAuthentication collected from the user, or null if none is provided.