Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Ctors](https://developer.android.com/reference/java/net/Authenticator#pubctors) \| [Methods](https://developer.android.com/reference/java/net/Authenticator#pubmethods) \| [Protected Methods](https://developer.android.com/reference/java/net/Authenticator#promethods) \| [Inherited Methods](https://developer.android.com/reference/java/net/Authenticator#inhmethods)

# Authenticator

` public abstract class Authenticator `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.net.Authenticator |

<br />

*** ** * ** ***

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 `https://developer.android.com/reference/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 `https://developer.android.com/reference/java/net/PasswordAuthentication` return value.

An instance of this concrete sub-class is then registered with the system by calling `https://developer.android.com/reference/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.
**See also:**

- `https://developer.android.com/reference/java/net/Authenticator#setDefault(java.net.Authenticator)`
- `https://developer.android.com/reference/java/net/Authenticator#getPasswordAuthentication()`

## Summary

| ### Public constructors ||
|---|---|
| ` https://developer.android.com/reference/java/net/Authenticator#Authenticator()() ` |

| ### Public methods ||
|---|---|
| ` static https://developer.android.com/reference/java/net/PasswordAuthentication` | ` https://developer.android.com/reference/java/net/Authenticator#requestPasswordAuthentication(java.lang.String,%20java.net.InetAddress,%20int,%20java.lang.String,%20java.lang.String,%20java.lang.String,%20java.net.URL,%20java.net.Authenticator.RequestorType)(https://developer.android.com/reference/java/lang/String host, https://developer.android.com/reference/java/net/InetAddress addr, int port, https://developer.android.com/reference/java/lang/String protocol, https://developer.android.com/reference/java/lang/String prompt, https://developer.android.com/reference/java/lang/String scheme, https://developer.android.com/reference/java/net/URL url, https://developer.android.com/reference/java/net/Authenticator.RequestorType reqType) ` Ask the authenticator that has been registered with the system for a password. |
| ` static https://developer.android.com/reference/java/net/PasswordAuthentication` | ` https://developer.android.com/reference/java/net/Authenticator#requestPasswordAuthentication(java.net.InetAddress,%20int,%20java.lang.String,%20java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/net/InetAddress addr, int port, https://developer.android.com/reference/java/lang/String protocol, https://developer.android.com/reference/java/lang/String prompt, https://developer.android.com/reference/java/lang/String scheme) ` Ask the authenticator that has been registered with the system for a password. |
| ` static https://developer.android.com/reference/java/net/PasswordAuthentication` | ` https://developer.android.com/reference/java/net/Authenticator#requestPasswordAuthentication(java.lang.String,%20java.net.InetAddress,%20int,%20java.lang.String,%20java.lang.String,%20java.lang.String)(https://developer.android.com/reference/java/lang/String host, https://developer.android.com/reference/java/net/InetAddress addr, int port, https://developer.android.com/reference/java/lang/String protocol, https://developer.android.com/reference/java/lang/String prompt, https://developer.android.com/reference/java/lang/String scheme) ` Ask the authenticator that has been registered with the system for a password. |
| ` static void` | ` https://developer.android.com/reference/java/net/Authenticator#setDefault(java.net.Authenticator)(https://developer.android.com/reference/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. |

| ### Protected methods ||
|---|---|
| ` https://developer.android.com/reference/java/net/PasswordAuthentication` | ` https://developer.android.com/reference/java/net/Authenticator#getPasswordAuthentication()() ` Called when password authorization is needed. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingHost()() ` Gets the `hostname` of the site or proxy requesting authentication, or `null` if not available. |
| ` final int` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingPort()() ` Gets the port number for the requested connection. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingPrompt()() ` Gets the prompt string given by the requestor. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingProtocol()() ` Give the protocol that's requesting the connection. |
| ` final https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingScheme()() ` Gets the scheme of the requestor (the HTTP scheme for an HTTP firewall, for example). |
| ` final https://developer.android.com/reference/java/net/InetAddress` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingSite()() ` Gets the `InetAddress` of the site requesting authorization, or `null` if not available. |
| ` https://developer.android.com/reference/java/net/URL` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestingURL()() ` Returns the URL that resulted in this request for authentication. |
| ` https://developer.android.com/reference/java/net/Authenticator.RequestorType` | ` https://developer.android.com/reference/java/net/Authenticator#getRequestorType()() ` Returns whether the requestor is a Proxy or a Server. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public constructors

### Authenticator

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Authenticator ()
```

<br />

<br />

## Public methods

### requestPasswordAuthentication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static PasswordAuthentication requestPasswordAuthentication (String host, 
                InetAddress addr, 
                int port, 
                String protocol, 
                String prompt, 
                String scheme, 
                URL url, 
                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.

<br />

| Parameters ||
|---|---|
| `host` | `String`: The hostname of the site requesting authentication. <br /> |
| `addr` | `InetAddress`: The InetAddress of the site requesting authorization, or null if not known. <br /> |
| `port` | `int`: the port for the requested connection <br /> |
| `protocol` | `String`: The protocol that's requesting the connection (`https://developer.android.com/reference/java/net/Authenticator#getRequestingProtocol()`) <br /> |
| `prompt` | `String`: A prompt string for the user <br /> |
| `scheme` | `String`: The authentication scheme <br /> |
| `url` | `URL`: The requesting URL that caused the authentication <br /> |
| `reqType` | `Authenticator.RequestorType`: The type (server or proxy) of the entity requesting authentication. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/PasswordAuthentication` | The username/password, or null if one can't be gotten. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `checkPermission` method doesn't allow the password authentication request. |

**See also:**

- `https://developer.android.com/reference/java/lang/SecurityManager#checkPermission(java.security.Permission)`
- `https://developer.android.com/reference/java/net/NetPermission`

### requestPasswordAuthentication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `addr` | `InetAddress`: The InetAddress of the site requesting authorization, or null if not known. <br /> |
| `port` | `int`: the port for the requested connection <br /> |
| `protocol` | `String`: The protocol that's requesting the connection (`https://developer.android.com/reference/java/net/Authenticator#getRequestingProtocol()`) <br /> |
| `prompt` | `String`: A prompt string for the user <br /> |
| `scheme` | `String`: The authentication scheme <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/PasswordAuthentication` | The username/password, or null if one can't be gotten. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `checkPermission` method doesn't allow the password authentication request. |

**See also:**

- `https://developer.android.com/reference/java/lang/SecurityManager#checkPermission(java.security.Permission)`
- `https://developer.android.com/reference/java/net/NetPermission`

### requestPasswordAuthentication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `host` | `String`: The hostname of the site requesting authentication. <br /> |
| `addr` | `InetAddress`: The InetAddress of the site requesting authentication, or null if not known. <br /> |
| `port` | `int`: the port for the requested connection. <br /> |
| `protocol` | `String`: The protocol that's requesting the connection (`https://developer.android.com/reference/java/net/Authenticator#getRequestingProtocol()`) <br /> |
| `prompt` | `String`: A prompt string for the user which identifies the authentication realm. <br /> |
| `scheme` | `String`: The authentication scheme <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/PasswordAuthentication` | The username/password, or null if one can't be gotten. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `checkPermission` method doesn't allow the password authentication request. |

**See also:**

- `https://developer.android.com/reference/java/lang/SecurityManager#checkPermission(java.security.Permission)`
- `https://developer.android.com/reference/java/net/NetPermission`

### setDefault

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Parameters ||
|---|---|
| `a` | `Authenticator`: The authenticator to be set. If a is `null` then any previously set authenticator is removed. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/lang/SecurityException` | if a security manager exists and its `checkPermission` method doesn't allow setting the default authenticator. |

**See also:**

- `https://developer.android.com/reference/java/lang/SecurityManager#checkPermission(java.security.Permission)`
- `https://developer.android.com/reference/java/net/NetPermission`

## Protected methods

### getPasswordAuthentication

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected PasswordAuthentication getPasswordAuthentication ()
```

Called when password authorization is needed. Subclasses should override the default implementation, which returns null.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/PasswordAuthentication` | The PasswordAuthentication collected from the user, or null if none is provided. <br /> |

### getRequestingHost

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final String getRequestingHost ()
```

Gets the `hostname` of the site or proxy requesting authentication, or `null` if not available.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the hostname of the connection requiring authentication, or null if it's not available. <br /> |

### getRequestingPort

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final int getRequestingPort ()
```

Gets the port number for the requested connection.

<br />

| Returns ||
|---|---|
| `int` | an `int` indicating the port for the requested connection. <br /> |

### getRequestingPrompt

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final String getRequestingPrompt ()
```

Gets the prompt string given by the requestor.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the prompt string given by the requestor (realm for http requests) <br /> |

### getRequestingProtocol

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
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.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the protocol, optionally followed by "/version", where version is a version number. <br /> |

**See also:**

- `https://developer.android.com/reference/java/net/URL#getProtocol()`

### getRequestingScheme

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final String getRequestingScheme ()
```

Gets the scheme of the requestor (the HTTP scheme for an HTTP firewall, for example).

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | the scheme of the requestor <br /> |

### getRequestingSite

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected final InetAddress getRequestingSite ()
```

Gets the `InetAddress` of the site requesting authorization, or `null` if not available.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/InetAddress` | the InetAddress of the site requesting authorization, or null if it's not available. <br /> |

### getRequestingURL

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected URL getRequestingURL ()
```

Returns the URL that resulted in this request for authentication.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/URL` | the requesting URL <br /> |

### getRequestorType

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
protected Authenticator.RequestorType getRequestorType ()
```

Returns whether the requestor is a Proxy or a Server.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/net/Authenticator.RequestorType` | the authentication type of the requestor <br /> |