Authenticator クラスは、URL にアクセスするために認証が必要な場合に使用されます。認証が必要であることが判明すると、ユーザーに同じ認証を求めるプロンプトが表示されるか、ハードコーディングされたユーザー名とパスワードが使用されます。
このクラスを使用するには、次の手順に従います。
-
Authenticator を拡張するクラスを作成します。これに「customAuth」という名前を付けます。 - getPasswordAuthentication() メソッドをオーバーライドします。このメソッドには、認証を要求しているエンティティの詳細を取得するためのいくつかのメソッドが含まれています。これらすべての方法については、後で詳しく説明します。
- Authenticator クラスの setDefault(Authenticator a) メソッドを使用して、http サーバーが認証を要求するときに使用するデフォルトのオーセンティケーターとして、新しく作成したサブクラスを設定します。
-
-
inetaddress が使用できない場合にホスト名を使用できる状況で使用できる、別のオーバーロードされたメソッドです。
-
認証を要求するサイトの URL のみがわかっており、inetaddress とホスト名がわかっていない場合に使用できる、別のオーバーロードされたメソッドです。
-
-
-
-
-
-
-
-
-
-
setDefault(Authenticator a) : HTTP サーバーが認証を必要とする場合に使用されるオーセンティケーターを設定します。
Syntax : public static void setDefault(Authenticator a) throws SecurityException Parameter : a : authenticator to be set as default Throws : 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 authentication port : port of requesting site protocol : protocol used for connection prompt : message for the user scheme : authentication scheme Throws : SecurityException : if security manager doesn't allow setting password authentication.
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 authentication addr : Inet address of the site asking for authentication port : port of requesting site protocol : protocol used for connection prompt : message for the user scheme : authentication scheme Throws : SecurityException : if security manager doesn't allow setting password authentication.
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 authentication addr : Inet address of the site asking for authentication port : port of requesting site protocol : protocol used for connection prompt : message for the user url : URL of the site requesting authentication scheme : authentication scheme Throws : SecurityException : if security manager doesn't allow setting password authentication.
getRequestingHost() : 認証を要求しているサイトのホスト名を返します。
Syntax : protected final String getRequestingHost()
getRequestingSite() : 認証を要求しているサイトの inetaddress を返します。
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() : このメソッドは、パスワード認証が必要な場合に呼び出されます。デフォルトのメソッドは常に null を返すため、すべてのサブクラスはこのメソッドをオーバーライドする必要があります。
Syntax : protected PasswordAuthentication getPasswordAuthentication()
getRequestingURL() : リクエスタの URL を返します。
Syntax : protected final URL getRequestingURL()
getRequestorType() : リクエスタがプロキシまたはサーバーの場合に返します。
Syntax : protected Authenticator.RequestorType getRequestorType()
-