Authenticator类用于需要身份验证才能访问某个URL的情况。一旦知道需要身份验证,它会提示用户输入相同的用户名和密码,或者使用一些硬编码的用户名和密码。 要使用此类,请遵循以下步骤-
null
- 创建一个扩展验证器的类。我们把它命名为customAuth。
- 重写getPasswordAuthentication()方法。此方法包含用于获取请求身份验证的实体的详细信息的几种方法。所有这些方法将在后面详细讨论。
- 使用authenticator类的setDefault(authenticator a)方法,将新创建的子类设置为http服务器请求身份验证时要使用的默认身份验证程序。
- 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
- 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.
- 另一种重载方法,可用于在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.
- 另一种重载方法,如果请求身份验证的站点的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.
- getRequestingHost() :返回请求身份验证的站点的主机名。
Syntax : protected final String getRequestingHost()
- getRequestingSite() :返回请求身份验证的站点的地址。
Syntax : protected final InetAddress getRequestingSite()
- getRequestingPort() :返回连接端口。
Syntax : protected final int getRequestingPort()
- getRequestingProtocol() :返回请求连接的协议。
Syntax : protected final String getRequestingProtocol()
- getRequestingPrompt() :返回请求者提示的消息。
Syntax : protected final String getRequestingPrompt()
- getRequestingScheme() :返回请求站点的方案。
Syntax : protected final String getRequestingScheme()
- getPasswordAuthentication() :需要密码身份验证时调用此方法。此默认方法必须始终重写所有子类。
Syntax : protected PasswordAuthentication getPasswordAuthentication()
- getRequestingURL() :返回请求者的url。
Syntax : protected final URL getRequestingURL()
- getRequestorType() :如果请求者是代理或服务器,则返回。
Syntax : protected Authenticator.RequestorType getRequestorType()
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END