logo

Java の Java.net.Authenticator クラス

Authenticator クラスは、URL にアクセスするために認証が必要な場合に使用されます。認証が必要であることが判明すると、ユーザーに同じ認証を求めるプロンプトが表示されるか、ハードコーディングされたユーザー名とパスワードが使用されます。 
このクラスを使用するには、次の手順に従います。 
 


  1. Authenticator を拡張するクラスを作成します。これに「customAuth」という名前を付けます。
  2. getPasswordAuthentication() メソッドをオーバーライドします。このメソッドには、認証を要求しているエンティティの詳細を取得するためのいくつかのメソッドが含まれています。これらすべての方法については、後で詳しく説明します。
  3. Authenticator クラスの setDefault(Authenticator a) メソッドを使用して、http サーバーが認証を要求するときに使用するデフォルトのオーセンティケーターとして、新しく作成したサブクラスを設定します。
      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

  1.  
  2. 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.

  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 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.

  1. 認証を要求するサイトの URL のみがわかっており、inetaddress とホスト名がわかっていない場合に使用できる、別のオーバーロードされたメソッドです。 
     
  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.

  1.  
  2. getRequestingHost(): 認証を要求しているサイトのホスト名を返します。 
     
  Syntax : protected final String getRequestingHost()  

  1.  
  2. getRequestingSite(): 認証を要求しているサイトの inetaddress を返します。 
     
  Syntax : protected final InetAddress getRequestingSite()  

  1.  
  2. getRequestingPort(): 接続ポートを返します。 
     
  Syntax : protected final int getRequestingPort()  

  1.  
  2. getRequestingProtocol(): 接続を要求するプロトコルを返します。 
     
  Syntax : protected final String getRequestingProtocol()  

  1.  
  2. getRequestingPrompt(): リクエスタが要求したメッセージを返します。 
     
  Syntax : protected final String getRequestingPrompt()  

  1.  
  2. getRequestingScheme(): 要求元サイトのスキームを返します。 
     
  Syntax : protected final String getRequestingScheme()  

  1.  
  2. getPasswordAuthentication(): このメソッドは、パスワード認証が必要な場合に呼び出されます。デフォルトのメソッドは常に null を返すため、すべてのサブクラスはこのメソッドをオーバーライドする必要があります。 
     
  Syntax : protected PasswordAuthentication getPasswordAuthentication()  

  1.  
  2. getRequestingURL(): リクエスタの URL を返します。 
     
  Syntax : protected final URL getRequestingURL()  

  1.  
  2. getRequestorType(): リクエスタがプロキシまたはサーバーの場合に返します。 
     
  Syntax : protected Authenticator.RequestorType getRequestorType()  

  1.  
クイズの作成