Spec-Zone .ru
спецификации, руководства, описания, API
JavaTM 2 Platform
Standard Edition

java.net
Class Authenticator

java.lang.Object
  |
  +--java.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:
JDK1.2
See Also:
java.net.Authenticator.setDefault(java.net.ConnectionAuthenticator), java.net.getPasswordAuthentication()

Constructor Summary
Authenticator()
           
 
Method Summary
protected  PasswordAuthentication getPasswordAuthentication()
          Called when password authorization is needed.
protected  int getRequestingPort()
           
protected  String getRequestingPrompt()
           
protected  String getRequestingProtocol()
          Give the protocol that's requesting the connection.
protected  String getRequestingScheme()
           
protected  InetAddress getRequestingSite()
           
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 void setDefault(Authenticator a)
          Sets the authenticator that will be used by the networking code when a proxy or an HTTP server asks for authenticator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Authenticator

public Authenticator()
Method Detail

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 authenticator. If an Authenticator has already been established as the current authenticator, no action will be taken. If the argument is null and no authenticator has been established, then no action is taken and the method simply returns.

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.

Typically, this method will be called exactly once, at system startup.

Parameters:
a - The authenticator
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 (@see java.net.Authenticator.getProtocol())
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

getRequestingSite

protected final InetAddress getRequestingSite()
Returns:
the InetAddress of the site requesting authorization, or null if it's not available.

getRequestingPort

protected final int getRequestingPort()
Returns:
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 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:
java.net.URL.getProtocol()

getRequestingPrompt

protected final String getRequestingPrompt()
Returns:
the prompt string given by the requestor (realm for http requests)

getRequestingScheme

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

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.

JavaTM 2 Platform
Standard Edition

Submit a bug or feature
Java, Java 2D, and JDBC are a trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1999 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.