Trieda Java.net.Authenticator v jazyku Java

Trieda Authenticator sa používa v prípadoch, keď je na návštevu nejakej adresy URL potrebná autentifikácia. Akonáhle je známe, že sa vyžaduje overenie, požiada používateľa o to isté alebo použije nejaké pevne zakódované používateľské meno a heslo. 
Ak chcete použiť túto triedu, postupujte podľa nasledujúcich krokov: 
 


  1. Vytvorte triedu, ktorá rozšíri Authenticator. Nazvime to customAuth.
  2. Prepíšte metódu getPasswordAuthentication(). Táto metóda obsahuje niekoľko metód na získanie podrobností o entite žiadajúcej o autentifikáciu. Všetky tieto metódy sú podrobne diskutované neskôr.
  3. Nastavte novovytvorenú podtriedu ako predvolený autentifikátor, ktorý sa má použiť, keď http server požaduje autentifikáciu pomocou metódy setDefault(Authenticator a) triedy Authenticator.
      setDefault(Authenticator a) : Nastaví autentifikátor, ktorý sa má použiť, keď HTTP server vyžaduje autentifikáciu. 
       
  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žiada autentifikátora registrovaného v systéme o heslo. Ak sa nenájde, vráti používateľské meno/heslo alebo hodnotu 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. Ďalšia preťažená metóda, ktorú možno použiť v situáciách, keď je možné použiť názov hostiteľa, ak nie je k dispozícii 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. Ďalšia preťažená metóda, ktorú je možné použiť, ak je známa iba adresa URL stránky požadujúcej autentifikáciu a nie inetaadresa a názov hostiteľa. 
     
  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() : vráti názov hostiteľa stránky požadujúcej overenie. 
     
  Syntax : protected final String getRequestingHost()   

  1.  
  2. getRequestingSite() : vráti inetaadresu stránky požadujúcej overenie. 
     
  Syntax : protected final InetAddress getRequestingSite()   

  1.  
  2. getRequestingPort() : vráti port pripojenia. 
     
  Syntax : protected final int getRequestingPort()   

  1.  
  2. getRequestingProtocol() : vráti protokol požadujúci pripojenie. 
     
  Syntax : protected final String getRequestingProtocol()   

  1.  
  2. getRequestingPrompt() : vráti správu vyžiadanú žiadateľom. 
     
  Syntax : protected final String getRequestingPrompt()   

  1.  
  2. getRequestingScheme() : vráti schému žiadajúcej stránky. 
     
  Syntax : protected final String getRequestingScheme()   

  1.  
  2. getPasswordAuthentication() : táto metóda sa volá, keď sa vyžaduje overenie hesla. Všetky podtriedy musia túto metódu prepísať, pretože predvolená metóda vždy vráti hodnotu null. 
     
  Syntax : protected PasswordAuthentication getPasswordAuthentication()   

  1.  
  2. getRequestingURL() : vráti adresu URL žiadateľa. 
     
  Syntax : protected final URL getRequestingURL()   

  1.  
  2. getRequestorType() : vráti, ak je žiadateľom proxy alebo server. 
     
  Syntax : protected Authenticator.RequestorType getRequestorType()   

  1.  
Vytvoriť kvíz