Most visited

Recently visited

Added in API level 1

Authenticator

public abstract class Authenticator
extends Object

java.lang.Object
   ↳ java.net.Authenticator


类Authenticator表示知道如何获取网络连接的身份验证的对象。 通常,它会通过提示用户提供信息来做到这一点。

应用程序通过在子类中重写getPasswordAuthentication()来使用此类。 此方法通常会使用各种getXXX()访问器方法来获取有关请求身份验证的实体的信息。 然后它必须通过与用户交互或通过其他非交互方式获取用户名和密码。 凭证然后以PasswordAuthentication返回值的PasswordAuthentication返回。

然后通过调用setDefault(Authenticator)将该具体子类的一个实例注册到系统中。 当需要身份验证时,系统将调用其中一个requestPasswordAuthentication()方法,该方法将调用注册对象的getPasswordAuthentication()方法。

所有请求身份验证的方法都有一个失败的默认实现。

也可以看看:

Summary

Nested classes

枚举 Authenticator.RequestorType

请求认证的实体的类型。

Public constructors

Authenticator()

Public methods

static PasswordAuthentication requestPasswordAuthentication(String host, InetAddress addr, int port, String protocol, String prompt, String scheme, URL url, Authenticator.RequestorType reqType)

询问已向系统注册的验证器是否有密码。

static PasswordAuthentication requestPasswordAuthentication(InetAddress addr, int port, String protocol, String prompt, String scheme)

询问已向系统注册的验证器是否有密码。

static PasswordAuthentication requestPasswordAuthentication(String host, InetAddress addr, int port, String protocol, String prompt, String scheme)

询问已向系统注册的验证器是否有密码。

static void setDefault(Authenticator a)

设置代理或HTTP服务器要求进行身份验证时将由网络代码使用的身份验证器。

Protected methods

PasswordAuthentication getPasswordAuthentication()

当需要密码授权时调用。

final String getRequestingHost()

获取 hostname的网站或代理请求验证的,或 null如果不可用。

final int getRequestingPort()

获取请求的连接的端口号。

final String getRequestingPrompt()

获取请求者给出的提示字符串。

final String getRequestingProtocol()

给出请求连接的协议。

final String getRequestingScheme()

获取请求方的方案(例如,HTTP防火墙的HTTP方案)。

final InetAddress getRequestingSite()

获取请求授权的站点的 InetAddress ,或者 null如果不可用)。

URL getRequestingURL()

返回导致此请求进行身份验证的URL。

Authenticator.RequestorType getRequestorType()

返回请求者是代理还是服务器。

Inherited methods

From class java.lang.Object

Public constructors

Authenticator

Added in API level 1
Authenticator ()

Public methods

requestPasswordAuthentication

Added in API level 1
PasswordAuthentication requestPasswordAuthentication (String host, 
                InetAddress addr, 
                int port, 
                String protocol, 
                String prompt, 
                String scheme, 
                URL url, 
                Authenticator.RequestorType reqType)

询问已向系统注册的验证器是否有密码。

首先,如果有安全管理器,则其checkPermission方法被调用并具有NetPermission("requestPasswordAuthentication")权限。 这可能会导致java.lang.SecurityException。

Parameters
host String: The hostname of the site requesting authentication.
addr InetAddress: The InetAddress of the site requesting authorization, or null if not known.
port int: the port for the requested connection
protocol String: The protocol that's requesting the connection (getRequestingProtocol())
prompt String: A prompt string for the user
scheme String: The authentication scheme
url URL: The requesting URL that caused the authentication
reqType Authenticator.RequestorType: The type (server or proxy) of the entity requesting authentication.
Returns
PasswordAuthentication 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.

也可以看看:

requestPasswordAuthentication

Added in API level 1
PasswordAuthentication requestPasswordAuthentication (InetAddress addr, 
                int port, 
                String protocol, 
                String prompt, 
                String scheme)

询问已向系统注册的验证器是否有密码。

首先,如果有安全管理器,则其checkPermission方法被调用并具有NetPermission("requestPasswordAuthentication")权限。 这可能会导致java.lang.SecurityException。

Parameters
addr InetAddress: The InetAddress of the site requesting authorization, or null if not known.
port int: the port for the requested connection
protocol String: The protocol that's requesting the connection (getRequestingProtocol())
prompt String: A prompt string for the user
scheme String: The authentication scheme
Returns
PasswordAuthentication 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.

也可以看看:

requestPasswordAuthentication

Added in API level 1
PasswordAuthentication requestPasswordAuthentication (String host, 
                InetAddress addr, 
                int port, 
                String protocol, 
                String prompt, 
                String scheme)

询问已向系统注册的验证器是否有密码。 这是请求密码的首选方法,因为在InetAddress不可用的情况下可以提供主机名。

首先,如果有安全管理器,则其checkPermission方法被调用并具有NetPermission("requestPasswordAuthentication")权限。 这可能会导致java.lang.SecurityException。

Parameters
host String: The hostname of the site requesting authentication.
addr InetAddress: The InetAddress of the site requesting authentication, or null if not known.
port int: the port for the requested connection.
protocol String: The protocol that's requesting the connection (getRequestingProtocol())
prompt String: A prompt string for the user which identifies the authentication realm.
scheme String: The authentication scheme
Returns
PasswordAuthentication 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.

也可以看看:

setDefault

Added in API level 1
void setDefault (Authenticator a)

设置代理或HTTP服务器要求进行身份验证时将由网络代码使用的身份验证器。

首先,如果有安全管理器,则其checkPermission方法被调用并具有NetPermission("setDefaultAuthenticator")权限。 这可能会导致java.lang.SecurityException。

Parameters
a Authenticator: 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.

也可以看看:

Protected methods

getPasswordAuthentication

Added in API level 1
PasswordAuthentication getPasswordAuthentication ()

当需要密码授权时调用。 子类应该覆盖默认实现,该实现返回null。

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

getRequestingHost

Added in API level 1
String getRequestingHost ()

获取 hostname的网站或代理请求验证的,或 null如果不可用。

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

getRequestingPort

Added in API level 1
int getRequestingPort ()

获取请求的连接的端口号。

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

getRequestingPrompt

Added in API level 1
String getRequestingPrompt ()

获取请求者给出的提示字符串。

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

getRequestingProtocol

Added in API level 1
String getRequestingProtocol ()

给出请求连接的协议。 通常这将基于URL,但在未来的JDK中,它可能是,例如,用于受密码保护的SOCKS5防火墙的“SOCKS”。

Returns
String the protcol, optionally followed by "/version", where version is a version number.

也可以看看:

getRequestingScheme

Added in API level 1
String getRequestingScheme ()

获取请求方的方案(例如,HTTP防火墙的HTTP方案)。

Returns
String the scheme of the requestor

getRequestingSite

Added in API level 1
InetAddress getRequestingSite ()

获取 InetAddress请求授权的站点,或者 null如果不可用。

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

getRequestingURL

Added in API level 1
URL getRequestingURL ()

返回导致此请求进行身份验证的URL。

Returns
URL the requesting URL

getRequestorType

Added in API level 1
Authenticator.RequestorType getRequestorType ()

返回请求者是代理还是服务器。

Returns
Authenticator.RequestorType the authentication type of the requestor

Hooray!