java.net
Class Authenticator

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.net.Authenticator

public abstract class Authenticator
extends Object sample code for java.lang.Object definition code for java.lang.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 overriding getPasswordAuthentication() sample code for java.net.Authenticator.getPasswordAuthentication() definition code for java.net.Authenticator.getPasswordAuthentication() in a sub-class. This method will typically use the various getXXX() accessor methods to get information about the entity requesting authentication. It must then acquire a username and password either by interacting with the user or through some other non-interactive means. The credentials are then returned as a PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication return value.

An instance of this concrete sub-class is then registered with the system by calling setDefault(Authenticator) sample code for java.net.Authenticator.setDefault(java.net.Authenticator) definition code for java.net.Authenticator.setDefault(java.net.Authenticator) . When authentication is required, the system will invoke one of the requestPasswordAuthentication() methods which in turn will call the getPasswordAuthentication() method of the registered object.

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

Since:
1.2
See Also:
setDefault(java.net.Authenticator) sample code for java.net.Authenticator.setDefault(java.net.Authenticator) definition code for java.net.Authenticator.setDefault(java.net.Authenticator) , getPasswordAuthentication() sample code for java.net.Authenticator.getPasswordAuthentication() definition code for java.net.Authenticator.getPasswordAuthentication()

Nested Class Summary
static class Authenticator.RequestorType sample code for java.net.Authenticator.RequestorType definition code for java.net.Authenticator.RequestorType
          The type of the entity requesting authentication.
 
Constructor Summary
Authenticator sample code for java.net.Authenticator.Authenticator() definition code for java.net.Authenticator.Authenticator() ()
           
 
Method Summary
protected  PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication getPasswordAuthentication sample code for java.net.Authenticator.getPasswordAuthentication() definition code for java.net.Authenticator.getPasswordAuthentication() ()
          Called when password authorization is needed.
protected  String sample code for java.lang.String definition code for java.lang.String getRequestingHost sample code for java.net.Authenticator.getRequestingHost() definition code for java.net.Authenticator.getRequestingHost() ()
          Gets the hostname of the site or proxy requesting authentication, or null if not available.
protected  int getRequestingPort sample code for java.net.Authenticator.getRequestingPort() definition code for java.net.Authenticator.getRequestingPort() ()
          Gets the port number for the requested connection.
protected  String sample code for java.lang.String definition code for java.lang.String getRequestingPrompt sample code for java.net.Authenticator.getRequestingPrompt() definition code for java.net.Authenticator.getRequestingPrompt() ()
          Gets the prompt string given by the requestor.
protected  String sample code for java.lang.String definition code for java.lang.String getRequestingProtocol sample code for java.net.Authenticator.getRequestingProtocol() definition code for java.net.Authenticator.getRequestingProtocol() ()
          Give the protocol that's requesting the connection.
protected  String sample code for java.lang.String definition code for java.lang.String getRequestingScheme sample code for java.net.Authenticator.getRequestingScheme() definition code for java.net.Authenticator.getRequestingScheme() ()
          Gets the scheme of the requestor (the HTTP scheme for an HTTP firewall, for example).
protected  InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress getRequestingSite sample code for java.net.Authenticator.getRequestingSite() definition code for java.net.Authenticator.getRequestingSite() ()
          Gets the InetAddress of the site requesting authorization, or null if not available.
protected  URL sample code for java.net.URL definition code for java.net.URL getRequestingURL sample code for java.net.Authenticator.getRequestingURL() definition code for java.net.Authenticator.getRequestingURL() ()
          Returns the URL that resulted in this request for authentication.
protected  Authenticator.RequestorType sample code for java.net.Authenticator.RequestorType definition code for java.net.Authenticator.RequestorType getRequestorType sample code for java.net.Authenticator.getRequestorType() definition code for java.net.Authenticator.getRequestorType() ()
          Returns whether the requestor is a Proxy or a Server.
static PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication requestPasswordAuthentication sample code for java.net.Authenticator.requestPasswordAuthentication(java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String) definition code for java.net.Authenticator.requestPasswordAuthentication(java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String) (InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr, int port, String sample code for java.lang.String definition code for java.lang.String  protocol, String sample code for java.lang.String definition code for java.lang.String  prompt, String sample code for java.lang.String definition code for java.lang.String  scheme)
          Ask the authenticator that has been registered with the system for a password.
static PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication requestPasswordAuthentication sample code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String) definition code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  host, InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr, int port, String sample code for java.lang.String definition code for java.lang.String  protocol, String sample code for java.lang.String definition code for java.lang.String  prompt, String sample code for java.lang.String definition code for java.lang.String  scheme)
          Ask the authenticator that has been registered with the system for a password.
static PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication requestPasswordAuthentication sample code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String, java.net.URL, java.net.Authenticator.RequestorType) definition code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String, java.net.URL, java.net.Authenticator.RequestorType) (String sample code for java.lang.String definition code for java.lang.String  host, InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr, int port, String sample code for java.lang.String definition code for java.lang.String  protocol, String sample code for java.lang.String definition code for java.lang.String  prompt, String sample code for java.lang.String definition code for java.lang.String  scheme, URL sample code for java.net.URL definition code for java.net.URL  url, Authenticator.RequestorType sample code for java.net.Authenticator.RequestorType definition code for java.net.Authenticator.RequestorType  reqType)
          Ask the authenticator that has been registered with the system for a password.
static void setDefault sample code for java.net.Authenticator.setDefault(java.net.Authenticator) definition code for java.net.Authenticator.setDefault(java.net.Authenticator) (Authenticator sample code for java.net.Authenticator definition code for java.net.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 sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

Authenticator sample code for java.net.Authenticator() definition code for java.net.Authenticator()

public Authenticator()
Method Detail

setDefault sample code for java.net.Authenticator.setDefault(java.net.Authenticator) definition code for java.net.Authenticator.setDefault(java.net.Authenticator)

public static void setDefault(Authenticator sample code for java.net.Authenticator definition code for java.net.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 sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkPermission method doesn't allow setting the default authenticator.
See Also:
SecurityManager.checkPermission(java.security.Permission) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , NetPermission sample code for java.net.NetPermission definition code for java.net.NetPermission

requestPasswordAuthentication sample code for java.net.Authenticator.requestPasswordAuthentication(java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String) definition code for java.net.Authenticator.requestPasswordAuthentication(java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String)

public static PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication  requestPasswordAuthentication(InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr,
                                                                   int port,
                                                                   String sample code for java.lang.String definition code for java.lang.String  protocol,
                                                                   String sample code for java.lang.String definition code for java.lang.String  prompt,
                                                                   String sample code for java.lang.String definition code for java.lang.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() sample code for java.net.Authenticator.getRequestingProtocol() definition code for java.net.Authenticator.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 sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkPermission method doesn't allow the password authentication request.
See Also:
SecurityManager.checkPermission(java.security.Permission) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , NetPermission sample code for java.net.NetPermission definition code for java.net.NetPermission

requestPasswordAuthentication sample code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String) definition code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String)

public static PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication  requestPasswordAuthentication(String sample code for java.lang.String definition code for java.lang.String  host,
                                                                   InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr,
                                                                   int port,
                                                                   String sample code for java.lang.String definition code for java.lang.String  protocol,
                                                                   String sample code for java.lang.String definition code for java.lang.String  prompt,
                                                                   String sample code for java.lang.String definition code for java.lang.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() sample code for java.net.Authenticator.getRequestingProtocol() definition code for java.net.Authenticator.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 sample code for java.lang.SecurityException definition code for java.lang.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) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , NetPermission sample code for java.net.NetPermission definition code for java.net.NetPermission

requestPasswordAuthentication sample code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String, java.net.URL, java.net.Authenticator.RequestorType) definition code for java.net.Authenticator.requestPasswordAuthentication(java.lang.String, java.net.InetAddress, int, java.lang.String, java.lang.String, java.lang.String, java.net.URL, java.net.Authenticator.RequestorType)

public static PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.PasswordAuthentication  requestPasswordAuthentication(String sample code for java.lang.String definition code for java.lang.String  host,
                                                                   InetAddress sample code for java.net.InetAddress definition code for java.net.InetAddress  addr,
                                                                   int port,
                                                                   String sample code for java.lang.String definition code for java.lang.String  protocol,
                                                                   String sample code for java.lang.String definition code for java.lang.String  prompt,
                                                                   String sample code for java.lang.String definition code for java.lang.String  scheme,
                                                                   URL sample code for java.net.URL definition code for java.net.URL  url,
                                                                   Authenticator.RequestorType sample code for java.net.Authenticator.RequestorType definition code for java.net.Authenticator.RequestorType  reqType)
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:
host - The hostname of the site requesting authentication.
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() sample code for java.net.Authenticator.getRequestingProtocol() definition code for java.net.Authenticator.getRequestingProtocol() )
prompt - A prompt string for the user
scheme - The authentication scheme
url - The requesting URL that caused the authentication
reqType - The type (server or proxy) of the entity requesting authentication.
Returns:
The username/password, or null if one can't be gotten.
Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkPermission method doesn't allow the password authentication request.
Since:
1.5
See Also:
SecurityManager.checkPermission(java.security.Permission) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , NetPermission sample code for java.net.NetPermission definition code for java.net.NetPermission

getRequestingHost sample code for java.net.Authenticator.getRequestingHost() definition code for java.net.Authenticator.getRequestingHost()

protected final String sample code for java.lang.String definition code for java.lang.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 sample code for java.net.Authenticator.getRequestingSite() definition code for java.net.Authenticator.getRequestingSite()

protected final InetAddress sample code for java.net.InetAddress definition code for java.net.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 sample code for java.net.Authenticator.getRequestingPort() definition code for java.net.Authenticator.getRequestingPort()

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

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

getRequestingProtocol sample code for java.net.Authenticator.getRequestingProtocol() definition code for java.net.Authenticator.getRequestingProtocol()

protected final String sample code for java.lang.String definition code for java.lang.String  getRequestingProtocol()
Give the protocol that's requesting the connection. Often this will be based on a URL, but in a future JDK 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() sample code for java.net.URL.getProtocol() definition code for java.net.URL.getProtocol()

getRequestingPrompt sample code for java.net.Authenticator.getRequestingPrompt() definition code for java.net.Authenticator.getRequestingPrompt()

protected final String sample code for java.lang.String definition code for java.lang.String  getRequestingPrompt()
Gets the prompt string given by the requestor.

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

getRequestingScheme sample code for java.net.Authenticator.getRequestingScheme() definition code for java.net.Authenticator.getRequestingScheme()

protected final String sample code for java.lang.String definition code for java.lang.String  getRequestingScheme()
Gets the scheme of the requestor (the HTTP scheme for an HTTP firewall, for example).

Returns:
the scheme of the requestor

getPasswordAuthentication sample code for java.net.Authenticator.getPasswordAuthentication() definition code for java.net.Authenticator.getPasswordAuthentication()

protected PasswordAuthentication sample code for java.net.PasswordAuthentication definition code for java.net.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.

getRequestingURL sample code for java.net.Authenticator.getRequestingURL() definition code for java.net.Authenticator.getRequestingURL()

protected URL sample code for java.net.URL definition code for java.net.URL  getRequestingURL()
Returns the URL that resulted in this request for authentication.

Returns:
the requesting URL
Since:
1.5

getRequestorType sample code for java.net.Authenticator.getRequestorType() definition code for java.net.Authenticator.getRequestorType()

protected Authenticator.RequestorType sample code for java.net.Authenticator.RequestorType definition code for java.net.Authenticator.RequestorType  getRequestorType()
Returns whether the requestor is a Proxy or a Server.

Returns:
the authentication type of the requestor
Since:
1.5