Třída Java.net.Authenticator v Javě

Třída Authenticator se používá v případech, kdy je k návštěvě nějaké adresy URL vyžadována autentizace. Jakmile je známo, že je vyžadováno ověření, požádá uživatele o totéž nebo použije pevně zakódované uživatelské jméno a heslo. 
Chcete-li použít tuto třídu, postupujte podle následujících kroků: 
 


  1. Vytvořte třídu, která rozšiřuje Authenticator. Pojmenujme to customAuth.
  2. Přepište metodu getPasswordAuthentication(). Tato metoda obsahuje několik metod pro získání podrobností o entitě žádající o ověření. Všechny tyto metody jsou podrobně diskutovány později.
  3. Nastavte nově vytvořenou podtřídu jako výchozí autentizátor, který se má použít, když http server požádá o autentizaci pomocí metody setDefault(Authenticator a) třídy Authenticator.
      setDefault(Authenticator a) : Nastaví ověřovací modul, který se má použít, když server HTTP vyžaduje ověření. 
       
  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() : Požádá ověřovatele registrovaného v systému o heslo. Vrátí uživatelské jméno/heslo nebo null, pokud nebylo nalezeno.
     
  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. Další přetížená metoda, kterou lze použít v situacích, kdy lze použít název hostitele, pokud není k dispozici 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. Další přetížená metoda, kterou lze použít, pokud je známa pouze adresa URL webu požadujícího autentizaci a nikoli inetaadresa a název hostitele. 
     
  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() : vrací název hostitele webu požadujícího ověření. 
     
  Syntax : protected final String getRequestingHost()   

  1.  
  2. getRequestingSite() : vrací inetaadresu webu požadujícího ověření. 
     
  Syntax : protected final InetAddress getRequestingSite()   

  1.  
  2. getRequestingPort() : vrací port připojení. 
     
  Syntax : protected final int getRequestingPort()   

  1.  
  2. getRequestingProtocol() : vrátí protokol požadující připojení. 
     
  Syntax : protected final String getRequestingProtocol()   

  1.  
  2. getRequestingPrompt() : vrátí zprávu vyžádanou žadatelem. 
     
  Syntax : protected final String getRequestingPrompt()   

  1.  
  2. getRequestingScheme() : vrátí schéma žádajícího webu. 
     
  Syntax : protected final String getRequestingScheme()   

  1.  
  2. getPasswordAuthentication() : tato metoda se volá, když je vyžadováno ověření heslem. Všechny podtřídy musí tuto metodu přepsat, protože výchozí metoda vždy vrací hodnotu null. 
     
  Syntax : protected PasswordAuthentication getPasswordAuthentication()   

  1.  
  2. getRequestingURL() : vrátí adresu URL žadatele. 
     
  Syntax : protected final URL getRequestingURL()   

  1.  
  2. getRequestorType() : vrátí, pokud je žadatelem proxy nebo server. 
     
  Syntax : protected Authenticator.RequestorType getRequestorType()   

  1.  
Vytvořit kvíz