模块 java.base
 java.net

类 Authenticator

java.lang.Object
java.net.Authenticator

public abstract class Authenticator extends Object
类 Authenticator 表示一个对象,该对象知道如何获取网络连接的身份验证。通常,它会通过提示用户输入信息来做到这一点。

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

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

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

自从:
1.2
参见:
  • 构造方法详细信息

    • Authenticator

      public Authenticator()
      子类调用的构造方法。
  • 方法详情

    • setDefault

      public static void setDefault(Authenticator  a)
      设置当代理或 HTTP 服务请求身份验证时网络代码将使用的身份验证器。

      首先,如果有安全管理器,它的 checkPermission 方法将以 NetPermission("setDefaultAuthenticator") 权限调用。这可能会导致 java.lang.SecurityException。

      参数:
      a - 要设置的验证器。如果 a 是 null,则删除任何先前设置的验证器。
      抛出:
      SecurityException - 如果存在安全管理器且其 checkPermission 方法不允许设置默认身份验证器。
      参见:
    • getDefault

      public static Authenticator  getDefault()
      获取默认验证器。首先,如果有安全管理器,它的 checkPermission 方法将以 NetPermission("requestPasswordAuthentication") 权限调用。这可能会导致 java.lang.SecurityException。然后返回默认验证器(如果已设置)。否则,返回 null
      返回:
      默认身份验证器,如果设置,null否则。
      抛出:
      SecurityException - 如果安全管理器存在且其 checkPermission 方法不允许请求密码身份验证。
      自从:
      9
      参见:
    • requestPasswordAuthentication

      public static PasswordAuthentication  requestPasswordAuthentication(InetAddress  addr, int port, String  protocol, String  prompt, String  scheme)
      向已在系统中注册的验证器询问密码。

      首先,如果有安全管理器,它的 checkPermission 方法将以 NetPermission("requestPasswordAuthentication") 权限调用。这可能会导致 java.lang.SecurityException。

      参数:
      addr - 请求授权的站点的 InetAddress,如果未知则为 null。
      port - 请求连接的端口
      protocol - 请求连接的协议 (getRequestingProtocol() )
      prompt - 用户的提示字符串
      scheme - 身份验证方案
      返回:
      用户名/密码,如果无法获取则为 null。
      抛出:
      SecurityException - 如果安全管理器存在且其 checkPermission 方法不允许密码身份验证请求。
      参见:
    • requestPasswordAuthentication

      public static PasswordAuthentication  requestPasswordAuthentication(String  host, InetAddress  addr, int port, String  protocol, String  prompt, String  scheme)
      向已在系统中注册的验证器询问密码。这是请求密码的首选方法,因为在 InetAddress 不可用的情况下可以提供主机名。

      首先,如果有安全管理器,它的 checkPermission 方法将以 NetPermission("requestPasswordAuthentication") 权限调用。这可能会导致 java.lang.SecurityException。

      参数:
      host - 请求身份验证的站点的主机名。
      addr - 请求身份验证的站点的 InetAddress,如果未知则为 null。
      port - 请求连接的端口。
      protocol - 请求连接的协议 (getRequestingProtocol() )
      prompt - 标识身份验证领域的用户提示字符串。
      scheme - 身份验证方案
      返回:
      用户名/密码,如果无法获取则为 null。
      抛出:
      SecurityException - 如果安全管理器存在且其 checkPermission 方法不允许密码身份验证请求。
      自从:
      1.4
      参见:
    • requestPasswordAuthentication

      public static 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。

      参数:
      host - 请求身份验证的站点的主机名。
      addr - 请求授权的站点的 InetAddress,如果未知则为 null。
      port - 请求连接的端口
      protocol - 请求连接的协议 (getRequestingProtocol() )
      prompt - 用户的提示字符串
      scheme - 身份验证方案
      url - 导致身份验证的请求 URL
      reqType - 请求身份验证的实体的类型(服务或代理)。
      返回:
      用户名/密码,如果无法获取则为 null。
      抛出:
      SecurityException - 如果安全管理器存在且其 checkPermission 方法不允许密码身份验证请求。
      自从:
      1.5
      参见:
    • requestPasswordAuthentication

      public static PasswordAuthentication  requestPasswordAuthentication(Authenticator  authenticator, String  host, InetAddress  addr, int port, String  protocol, String  prompt, String  scheme, URL  url, Authenticator.RequestorType  reqType)
      向给定的 authenticator 询问密码。如果给定的 authenticator 为空,则使用已使用 setDefault 向系统注册的身份验证器(如果有)。

      首先,如果有安全管理器,它的 checkPermission 方法将以 NetPermission("requestPasswordAuthentication") 权限调用。这可能会导致 java.lang.SecurityException。

      参数:
      authenticator - 身份验证器,或 null
      host - 请求身份验证的站点的主机名。
      addr - 请求授权的站点的 InetAddress,如果未知则为 null。
      port - 请求连接的端口
      protocol - 请求连接的协议 (getRequestingProtocol() )
      prompt - 用户的提示字符串
      scheme - 身份验证方案
      url - 导致身份验证的请求 URL
      reqType - 请求身份验证的实体的类型(服务或代理)。
      返回:
      用户名/密码,如果无法获取,则为 null
      抛出:
      SecurityException - 如果安全管理器存在且其 checkPermission 方法不允许密码身份验证请求。
      自从:
      9
      参见:
    • requestPasswordAuthenticationInstance

      public PasswordAuthentication  requestPasswordAuthenticationInstance(String  host, InetAddress  addr, int port, String  protocol, String  prompt, String  scheme, URL  url, Authenticator.RequestorType  reqType)
      向此身份验证器询问密码。
      参数:
      host - 请求身份验证的站点的主机名。
      addr - 请求授权的站点的 InetAddress,如果未知则为 null。
      port - 请求连接的端口
      protocol - 请求连接的协议 (getRequestingProtocol() )
      prompt - 用户的提示字符串
      scheme - 身份验证方案
      url - 导致身份验证的请求 URL
      reqType - 请求身份验证的实体的类型(服务或代理)。
      返回:
      用户名/密码,如果无法获取则为 null
      自从:
      9
    • getRequestingHost

      protected final String  getRequestingHost()
      获取请求身份验证的站点或代理的 hostname,如果不可用,则获取 null
      返回:
      需要身份验证的连接的主机名,如果不可用则为 null。
      自从:
      1.4
    • getRequestingSite

      protected final InetAddress  getRequestingSite()
      获取请求授权的站点的 InetAddress,如果不可用,则获取 null
      返回:
      请求授权的站点的 InetAddress,如果不可用,则为 null。
    • getRequestingPort

      protected final int getRequestingPort()
      获取所请求连接的端口号。
      返回:
      int 指示所请求连接的端口。
    • getRequestingProtocol

      protected final String  getRequestingProtocol()
      提供请求连接的协议。通常这将基于 URL,但在未来的 JDK 中,它可能是,例如,“SOCKS”,用于受密码保护的 SOCKS5 防火墙。
      返回:
      协议,可选地后跟“/version”,其中 version 是版本号。
      参见:
    • getRequestingPrompt

      protected final String  getRequestingPrompt()
      获取请求者给出的提示字符串。
      返回:
      请求者给出的提示字符串(http请求的realm)
    • getRequestingScheme

      protected final String  getRequestingScheme()
      获取请求者的方案(例如,HTTP 防火墙的 HTTP 方案)。
      返回:
      请求者的计划
    • getPasswordAuthentication

      protected PasswordAuthentication  getPasswordAuthentication()
      需要密码授权时调用。子类应该重写默认实现,它返回 null。
      返回:
      从用户收集的 PasswordAuthentication,如果未提供则为 null。
    • getRequestingURL

      protected URL  getRequestingURL()
      返回导致此身份验证请求的 URL。
      返回:
      请求网址
      自从:
      1.5
    • getRequestorType

      protected Authenticator.RequestorType  getRequestorType()
      返回请求者是代理还是服务。
      返回:
      请求者的身份验证类型
      自从:
      1.5