JAVA网Java中的验证器类

Authenticator类用于需要身份验证才能访问某个URL的情况。一旦知道需要身份验证,它会提示用户输入相同的用户名和密码,或者使用一些硬编码的用户名和密码。 要使用此类,请遵循以下步骤-

null
  1. 创建一个扩展验证器的类。我们把它命名为customAuth。
  2. 重写getPasswordAuthentication()方法。此方法包含用于获取请求身份验证的实体的详细信息的几种方法。所有这些方法将在后面详细讨论。
  3. 使用authenticator类的setDefault(authenticator a)方法,将新创建的子类设置为http服务器请求身份验证时要使用的默认身份验证程序。
    1. setDefault(验证器a): 设置HTTP服务器需要身份验证时要使用的身份验证程序。
Syntax : public static void setDefault(Authenticator a)                 throws SecurityExceptionParameter :a : authenticator to be set as defaultThrows :SecurityException : if security manager doesn't allow setting default authenticator
  1. requestPasswordAuthentication(): 询问向系统注册的身份验证人密码。返回用户名/密码,如果找不到,则返回null。
Syntax : public static PasswordAuthentication requestPasswordAuthentication(                                                   InetAddress addr,                                                   int port,                                                   String protocol,                                                   String prompt,                                                   String scheme)Parameter :addr : Inet address of the site asking for authenticationport : port of requesting siteprotocol : protocol used for connectionprompt : message for the userscheme : authentication schemeThrows :SecurityException : if security manager doesn't allowsetting password authentication.
  1. 另一种重载方法,可用于在inetaddress不可用时使用主机名的情况。
Syntax : public static PasswordAuthentication requestPasswordAuthentication(                                                   String host,                                                   InetAddress addr,                                                   int port,                                                   String protocol,                                                   String prompt,                                                   String scheme)Parameter :host : hostname of the site asking for authenticationaddr : Inet address of the site asking for authenticationport : port of requesting siteprotocol : protocol used for connectionprompt : message for the userscheme : authentication schemeThrows :SecurityException : if security manager doesn't allow setting password authentication.
  1. 另一种重载方法,如果请求身份验证的站点的URL只已知,而不知道地址和主机名,则可以使用该方法。
Syntax : public static PasswordAuthentication requestPasswordAuthentication(                                                   String host,                                                   InetAddress addr,                                                   int port,                                                   String protocol,                                                   String prompt,                                                   URL url,                                                   String scheme)Parameter :host : hostname of the site asking for authenticationaddr : Inet address of the site asking for authenticationport : port of requesting siteprotocol : protocol used for connectionprompt : message for the userurl : URL of the site requesting authenticationscheme : authentication schemeThrows :SecurityException : if security manager doesn't allow setting password authentication.
  1. getRequestingHost() :返回请求身份验证的站点的主机名。
Syntax : protected final String getRequestingHost()
  1. getRequestingSite() :返回请求身份验证的站点的地址。
Syntax : protected final InetAddress getRequestingSite()
  1. getRequestingPort() :返回连接端口。
Syntax : protected final int getRequestingPort()
  1. getRequestingProtocol() :返回请求连接的协议。
Syntax : protected final String getRequestingProtocol()
  1. getRequestingPrompt() :返回请求者提示的消息。
Syntax : protected final String getRequestingPrompt()
  1. getRequestingScheme() :返回请求站点的方案。
Syntax : protected final String getRequestingScheme()
  1. getPasswordAuthentication() :需要密码身份验证时调用此方法。此默认方法必须始终重写所有子类。
Syntax : protected PasswordAuthentication getPasswordAuthentication()
  1. getRequestingURL() :返回请求者的url。
Syntax : protected final URL getRequestingURL()
  1. getRequestorType() :如果请求者是代理或服务器,则返回。
Syntax : protected Authenticator.RequestorType getRequestorType()
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享