Klasa Java.net.Authenticator w Javie

Klasa Authenticator jest używana w przypadkach, gdy wymagane jest uwierzytelnienie, aby odwiedzić jakiś adres URL. Gdy wiadomo, że wymagane jest uwierzytelnienie, użytkownik monituje o to samo lub używa zakodowanej na stałe nazwy użytkownika i hasła. 
Aby skorzystać z tej klasy, należy wykonać następujące kroki: 
 


  1. Utwórz klasę, która rozszerza moduł uwierzytelniający. Nazwijmy to niestandardoweAuth.
  2. Zastąp metodę getPasswordAuthentication(). Ta metoda zawiera kilka metod uzyskania szczegółów podmiotu żądającego uwierzytelnienia. Wszystkie te metody zostaną szczegółowo omówione w dalszej części.
  3. Ustaw nowo utworzoną podklasę jako domyślny moduł uwierzytelniający, który będzie używany, gdy serwer http poprosi o uwierzytelnienie za pomocą metody setDefault(Authenticator a) klasy Authenticator.
      setDefault(Authenticator a): Ustawia moduł uwierzytelniający, który będzie używany, gdy serwer HTTP wymaga uwierzytelnienia. 
       
  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() : Prosi osobę uwierzytelniającą zarejestrowaną w systemie o podanie hasła. Zwraca nazwę użytkownika/hasło lub wartość null, jeśli nie została znaleziona.
     
  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. Kolejna przeciążona metoda, której można użyć w sytuacjach, gdy można użyć nazwy hosta, jeśli adres ineta jest niedostępny. 
     
  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. Kolejna przeciążona metoda, której można użyć, jeśli znany jest tylko adres URL witryny żądającej uwierzytelnienia, a nie adres i nazwa hosta. 
     
  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() : zwraca nazwę hosta witryny żądającej uwierzytelnienia. 
     
  Syntax : protected final String getRequestingHost()   

  1.  
  2. getRequestingSite() : zwraca adres wejściowy witryny żądającej uwierzytelnienia. 
     
  Syntax : protected final InetAddress getRequestingSite()   

  1.  
  2. getRequestingPort() : zwraca port połączenia. 
     
  Syntax : protected final int getRequestingPort()   

  1.  
  2. getRequestingProtocol() : zwraca protokół żądający połączenia. 
     
  Syntax : protected final String getRequestingProtocol()   

  1.  
  2. getRequestingPrompt() : zwraca komunikat monitowany przez requestera. 
     
  Syntax : protected final String getRequestingPrompt()   

  1.  
  2. getRequestingScheme() : zwraca schemat strony żądającej. 
     
  Syntax : protected final String getRequestingScheme()   

  1.  
  2. getPasswordAuthentication() : ta metoda jest wywoływana, gdy wymagane jest uwierzytelnienie hasłem. Wszystkie podklasy muszą zastąpić tę metodę, ponieważ metoda domyślna zawsze zwraca wartość null. 
     
  Syntax : protected PasswordAuthentication getPasswordAuthentication()   

  1.  
  2. getRequestingURL() : zwraca adres URL osoby żądającej. 
     
  Syntax : protected final URL getRequestingURL()   

  1.  
  2. getRequestorType() : zwraca, jeśli obiekt żądający jest serwerem proxy lub serwerem. 
     
  Syntax : protected Authenticator.RequestorType getRequestorType()   

  1.  
Utwórz quiz