Java Authenticator 类

2025年3月20日 | 阅读 4 分钟

Authenticator 类代表了一个知道如何获取网络连接认证的对象。通常,它通过提示用户输入信息来完成。应用程序通过在子类中重写 getPasswordAuthentication() 方法来使用这个类。这个方法通常使用各种 getXXX() 访问器方法来获取有关请求认证的实体的有关信息。然后,它必须通过与用户交互或通过其他非交互方式获取用户名和密码。然后,凭据将作为 PasswordAuthentication 的返回值返回。

Java ForkJoinPool 类声明

Authenticator 方法列表

方法描述
1.protected PasswordAuthentication getPasswordAuthentication()Authenticator 类的 getPasswordAuthentication() 方法在需要密码授权时被调用。子类应覆盖默认的 getPasswordAuthentication() 实现,该实现返回 null。
2.protected final String getRequestingHost()Authenticator 类的 getRequestingHost() 方法获取请求认证的站点或代理的主机名,如果不可用则返回 null。
3.protected final int getRequestingPort()Authenticator 类的 getRequestingPort() 方法获取请求连接的端口号。
4.protected final String getRequestingPrompt()Authenticator 类的 getRequestingPrompt() 方法获取请求者提供的提示字符串。
5.protected final int getRequestingPort()Authenticator 类的 getRequestingProtocol() 方法提供请求连接的协议。这将基于 URL。
6.protected final String getRequestingScheme()Authenticator 类的 getRequestingScheme() 方法获取请求者的方案(例如,HTTP 防火墙的 HTTP 方案)。
7.protected final InetAddress getRequestingSite()Authenticator 类的 getRequestingSite() 方法获取请求授权的站点的 InetAddress,如果不可用则返回 null。
8.protected URL getRequestingURL()Authenticator 类的 getRequestingURL() 方法返回导致此认证请求的 URL。
9.public static void setDefault(Authenticator a)Authenticator 类的 setDefault() 方法设置当代理或 HTTP 服务器请求认证时网络代码将使用的认证器。
10.protected Authenticator.RequestorType getRequestorType()Authenticator 类的 getRequestorType() 方法返回请求者是代理还是服务器。

Java Authenticator 类示例:getRequestingPort ()

输出

Host using url.getHost ::www.TpointTech.com
Port Requesting :8080
// Source code of https://tpointtech.cn is printed. 

Java ForkJoinPool 类示例:getRequestingProtocol()

输出

Protocol Used:  https
Host using url.getHost ::www.TpointTech.com
Protocol:null
// Source code of https://tpointtech.cn is printed.  

Java ForkJoinPool 类示例:setDefault ()

输出

Port info ::-1
URL :null
// Source code of https://tpointtech.cn is printed.